C# in Async Task change Label Text

前端 未结 5 719
清酒与你
清酒与你 2021-01-03 09:50

The following Code does not change the Text and stops executing the Task

private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = \         


        
5条回答
  •  孤城傲影
    2021-01-03 10:32

    for accessing a GUI control through a second thread you need to invoke. following example shows how to set a label's text properly

      private void setLabel1TextSafe(string txt)
      { 
           if(label1.InvokeRequired)
               label1.Invoke(new Action(() => label1.Text = txt));
           else
               label1.Text = txt;
      }
    

    I hope this solves your problem

提交回复
热议问题