Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.Windows.Forms;  namespace WindowsFormsApplication1 {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }          private async void button1_Click(object sender, EventArgs e)         {             await Run();         }          private async Task Run()         {             await Task.Run(async () => {                 await File.AppendText("temp.dat").WriteAsync("a");                 label1.Text = "test";             });             }     } } 

This is a simplified version of the real application I'm working on. I was under the impression that by using async/await in my Task.Run I could set the label1.Text property. However, when running this code I get the error that I'm not on the UI thread and I can't access the control.

Why can't I access the label control?

like image 372
Wouter de Kort Avatar asked Sep 26 '13 12:09

Wouter de Kort


2 Answers

When you use Task.Run(), you're saing that you don't want the code to run on the current context, so that's exactly what happens.

But there is no need to use Task.Run() in your code. Correctly written async methods won't block the current thread, so you can use them from the UI thread directly. If you do that, await will make sure the method resumes back on the UI thread.

This means that if you write your code like this, it will work:

private async void button1_Click(object sender, EventArgs e) {     await Run(); }  private async Task Run() {     await File.AppendText("temp.dat").WriteAsync("a");     label1.Text = "test"; } 
like image 187
svick Avatar answered Sep 24 '22 05:09

svick


Try this:

replace

label1.Text = "test"; 

with

SetLabel1Text("test"); 

and add the following to your class:

private void SetLabel1Text(string text) {   if (InvokeRequired)   {     Invoke((Action<string>)SetLabel1Text, text);     return;   }   label1.Text = text; } 

The InvokeRequired returns true if you are NOT on the UI thread. The Invoke() method takes the delegate and parameters, switches to the UI thread and then calls the method recursively. You return after the Invoke() call because the method has already been called recursively prior to the Invoke() returning. If you happen to be on the UI thread when the method is called, the InvokeRequired is false and the assignment is performed directly.

like image 41
Metro Avatar answered Sep 20 '22 05:09

Metro