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

后端 未结 6 1375
忘了有多久
忘了有多久 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:05

    Why do you use Task.Run? that start a new worker thread (cpu bound), and it causes your problem.

    you should probably just do that:

        private async Task Run()
        {
            await File.AppendText("temp.dat").WriteAsync("a");
            label1.Text = "test";    
        }
    

    await ensure you will continue on the same context except if you use .ConfigureAwait(false);

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2020-11-29 05:24

    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.

    0 讨论(0)
  • 2020-11-29 05:31

    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";
    }
    
    0 讨论(0)
  • 2020-11-29 05:31

    Try this

    private async Task Run()
    {
        await Task.Run(async () => {
           await File.AppendText("temp.dat").WriteAsync("a");
           });
        label1.Text = "test";
    }
    

    Or

    private async Task Run()
    {
        await File.AppendText("temp.dat").WriteAsync("a");        
        label1.Text = "test";
    }
    

    Or

    private async Task Run()
    {
        var task = Task.Run(async () => {
           await File.AppendText("temp.dat").WriteAsync("a");
           });
        var continuation = task.ContinueWith(antecedent=> label1.Text = "test",TaskScheduler.FromCurrentSynchronizationContext());
        await task;//I think await here is redundant        
    }
    

    async/await doesn't guarantee that it will run in UI thread. await will capture the current SynchronizationContext and continues execution with the captured context once the task completed.

    So in your case you have a nested await which is inside Task.Run hence second await will capture the context which is not going to be UiSynchronizationContext because it is being executed by WorkerThread from ThreadPool.

    Does this answers your question?

    0 讨论(0)
  • 2020-11-29 05:31

    I am going to give you my latest answer that I have given for async understanding.

    The solution is as you know that when you are calling async method you need to run as a task.

    Here is a quick console app code that you can use for your reference, it will make it easy for you to understand the concept.

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Starting Send Mail Async Task");
            Task task = new Task(SendMessage);
            task.Start();
            Console.WriteLine("Update Database");
            UpdateDatabase();
    
            while (true)
            {
                // dummy wait for background send mail.
                if (task.Status == TaskStatus.RanToCompletion)
                {
                    break;
                }
            }
    
        }
    
        public static async void SendMessage()
        {
            // Calls to TaskOfTResult_MethodAsync
            Task<bool> returnedTaskTResult = MailSenderAsync();
            bool result = await returnedTaskTResult;
    
            if (result)
            {
                UpdateDatabase();
            }
    
            Console.WriteLine("Mail Sent!");
        }
    
        private static void UpdateDatabase()
        {
            for (var i = 1; i < 1000; i++) ;
            Console.WriteLine("Database Updated!");
        }
    
        private static async Task<bool> MailSenderAsync()
        {
            Console.WriteLine("Send Mail Start.");
            for (var i = 1; i < 1000000000; i++) ;
            return true;
        }
    }
    

    Here I am trying to initiate task called send mail. Interim I want to update database, while the background is performing send mail task.

    Once the database update has happened, it is waiting for the send mail task to be completed. However, with this approach it is quite clear that I can run task at the background and still proceed with original (main) thread.

    0 讨论(0)
提交回复
热议问题