Task chaining (wait for the previous task to completed)

前端 未结 3 1009
一整个雨季
一整个雨季 2020-12-03 08:06
var tasks = new List();

foreach (var guid in guids)
{
    var task = new Task( ...);
    tasks.Add(task);
}

foreach (var task in tasks)
{
    task.Star         


        
3条回答
  •  时光说笑
    2020-12-03 08:40

    This is not Task Chaining.

    You need to do Task chaining using ContinueWith. Last task would need to update the UI.

    Task.Factory.StartNew( () => DoThis())
       .ContinueWith((t1) => DoThat())
       .ContinueWith((t2) => UpdateUi(), 
           TaskScheduler.FromCurrentSynchronizationContext());
    

    Note the last line has TaskScheduler.FromCurrentSynchronizationContext() this will ensure task will run in the synchronization context (UI Thread).

提交回复
热议问题