C# in Async Task change Label Text

前端 未结 5 720
清酒与你
清酒与你 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:33

    Try this. You don't need to fire a new thread to invoke the async method. compiler will do it for you.

    private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "Test";
            MyAsyncMethod();
        }
    public async Task MyAsyncMethod()
    {
        return await Task.Run(() =>{
        label1.Text = "";
        //everything from here on will not be executed
         }
    }
    

提交回复
热议问题