is possible to run code asynchronously in ContinueWith(), when ContinueWith uses FromCurrentSynchronizationContext TaskScheduler?

14 views Asked by At

Is possible to somehow force action calcData to run asynchronously in ContinueWith?

Action loadData = () => { Thread.Sleep(200); Console.WriteLine("Used thread: " + Thread.CurrentThread.ManagedThreadId); /* Load data from DB*/ };
Action showData = () => { Thread.Sleep(100); Console.WriteLine("Used thread: " + Thread.CurrentThread.ManagedThreadId); /* Show data */ };
Action calcData = () => { Thread.Sleep(100); Console.WriteLine("Used thread: " + Thread.CurrentThread.ManagedThreadId); /* Calculate some fields*/ };

var task1 = new Task(loadData);

task1.Start();
task1.ContinueWith((antecedent) =>
{
    showData();

    var task2 = new Task(calcData);

    task2.Start();
}, TaskScheduler.FromCurrentSynchronizationContext());

Output:

Used thread: 18
Used thread: 1
Used thread: 1
0

There are 0 answers