Set value of label with C# Cross Threading

前端 未结 2 2032
孤城傲影
孤城傲影 2021-01-02 02:41

I need help with setting/changing the value of a label in my C# program whenever I try it an error occurs saying I need to cross thread it all. Can anyone write some code to

2条回答
  •  孤城傲影
    2021-01-02 03:43

    Try the following to update the value

    label5.Invoke((MethodInvoker)(() => label5.Text = "Requested" + repeats + "Times"));
    

    The Invoke method (from Control.Invoke) will cause the passed in delegate to be run on the thread which the given Control is affinitized to. In this case it will cause it to run on the GUI thread of your application and hence make the update safe.

提交回复
热议问题