Is TaskScheduler.Current and TaskScheduler.FromCurrentSynchronizationContext () the difference?

I have a task to get products from a database and actions ContinueWiththat use some modification of the user interface, so I had a problem because the task created a new thread, and the modification of the user interface was not performed in the user interface thread.

I tried using this fix:

var currentScheduler = TaskScheduler.Current;

Task.Factory.StartNew(() =>
{    
    // get products   
}).ContinueWith((x) => handleProductsArrived(x.Result, x.Exception), currentScheduler);

but it didn’t work. I check and ContinueWithdid not execute in the stream from currentScheduler, but in another.

I discovered this method:

Task.Factory.StartNew(() =>
{
    // get products
}).ContinueWith((x) => handleProductsArrived(x.Result, x.Exception), TaskScheduler.FromCurrentSynchronizationContext());

and it works. So what are the differences? Why didn't my first code work? Thank!

+5
source share
1 answer

From the documentation for TaskScheduler.Current:

, Current .

:

PLINQ .NET Framework ThreadPool .

, TaskScheduler.Current, , , .

TaskScheduler.FromCurrentSynchronizationContext(), - Windows Forms WPF ( ) , .

, : . .

, # 5 async/await, .

+12

All Articles