Continue the task after completing all tasks

In some class I want to load 2 collections asynchronously using Task and stop busyindicator

I'm trying something like this

var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
WaitingIndicatorViewModel.IsBusy = true;
var loadData1 = new Task<ObservableCollection<Data1>>(GetData1FromService).ContinueWith(t => Data1Collection = t.Result, uiScheduler);
var loadData2 = new Task<ObservableCollection<Data2>>(GetData2FromService).ContinueWith(t => Data2Collection = t.Result, uiScheduler);

Task.Factory.StartNew(() =>{
                loadData1.Start();//<--Exception here           
                loadData2.Start();
                Task.WaitAll(loadData1, loadData2);
        })
.ContinueWith(o => WaitingIndicatorViewModel.IsBusy = false, uiScheduler);

But this exception excludes InvalidOperationException:Start may not be called on a continuation task.

Why does this not work, and how can I start the continue task after completing both tasks without blocking the current thread?

+5
source share
1 answer

Instead:

var loadData1 = new Task<ObservableCollection<Data1>>(GetData1FromService)
               .ContinueWith(t => Data1Collection = t.Result, uiScheduler);

I think you mean:

var loadData1 = new Task<ObservableCollection<Data1>>(GetData1FromService);
loadData1.ContinueWith(t => Data1Collection = t.Result, uiScheduler);

Now you can (later) call:

loadData1.Start();

, loadData1 . loadData1 ContinueWith, - ( , ).

. , ContinueWith .

+15

All Articles