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!
source
share