WHAT IS TASK CONTINUATION ON UI THREAD?

Goti Bandu

Total Post:119

Points:835
Posted by  Goti Bandu
OOPS 
 1202  View(s)
Ratings:
Rate this:

Is there a 'standard' way to specify that a task continuation should run on the thread from which the initial task was created?

Currently, I have the code below - it is working but keeping track of the dispatcher and creating a second Action seems like unnecessary overhead.

dispatcher = Dispatcher.CurrentDispatcher;
            Task task = Task.Factory.StartNew(() =>
            {
                DoLongRunningWork();
            });
 
            Task UITask= task.ContinueWith(() =>
            {
                dispatcher.Invoke(new Action(() =>
                {
                    this.TextBlock1.Text = "Complete";
                }
            });

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: What is Task continuation on UI thread?

    Call the continuation with TaskScheduler.FromCurrentSynchronizationContext():

    Task UITask = task.ContinueWith(() =>
                {
                    this.TextBlock1.Text = "Complete";
                }, TaskScheduler.FromCurrentSynchronizationContext());

      Modified On Apr-05-2018 11:31:32 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!