Solve a cross-threading Exception in WinForms

后端 未结 6 1789
北荒
北荒 2020-12-01 17:14

Presently I\'m working with WinForms(in C#) and I have to run the application in the background. For this purpose I\'m using asynchronous. When I run the application it\'s s

6条回答
  •  渐次进展
    2020-12-01 18:05

    You need to check if Invoke is required for the control you're trying to update. Something like this:

    Action setterCallback = (toSet, text) => toSet.Text = text;
    
    void SetControlText(Control toSet, string text) {
      if (this.InvokeRequired) {
        this.Invoke(setterCallback, toSet, text);
      }
      else {
        setterCallback(toSet, text);
      }
    }
    

提交回复
热议问题