Accessing UI controls in Task.Run with async/await on WinForms

后端 未结 6 1467
忘了有多久
忘了有多久 2020-11-29 04:57

I have the following code in a WinForms application with one button and one label:

using System;
using System.IO;
using System.Threading.Tasks;
using System.         


        
6条回答
  •  [愿得一人]
    2020-11-29 05:09

    Because it's on a different thread and cross-thread calls aren't allowed.

    You will need to pass on the "context" to the thread you are starting. See an example here: http://reedcopsey.com/2009/11/17/synchronizing-net-4-tasks-with-the-ui-thread/

提交回复
热议问题