Update UI Label when using Task.Factory.StartNew

后端 未结 2 1091
不思量自难忘°
不思量自难忘° 2020-12-14 02:38

I am trying to make my UI more responsive in my WPF app. I spawn a new thread using

Task.Factory.StartNew( () => RecurseAndDeleteStart() );
相关标签:
2条回答
  • 2020-12-14 03:13

    Since it's WPF, you can use the Dispatcher and call Dispatcher.BeginInvoke to marshal the call back to the UI thread to update the label.

    Alternatively, you can pass a TaskScheduler into your method, and use it to update the label as follows:

    // This line needs to happen on the UI thread...
    TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    
    Task.Factory.StartNew( () => RecurseAndDeleteStart(uiScheduler) );
    

    Then, inside your method, when you want to update a label, you could do:

    Task.Factory.StartNew( () => 
      {
          theLabel.Text = "Foo";
      }, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
    

    This will push the call back onto the UI thread's synchronization context.

    0 讨论(0)
  • 2020-12-14 03:30

    You have to use the label.Dispatcher.BeginInvoke(delegate) to invoke anything from a different thread that will change the contents of the label.

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