In my WPF application, I need to run 2 long running tasks in parallel, both which return data that needs to be shown in the UI.
I have a a property in my view model called IsBusy, which should be true until both tasks have completed.
How to I get a notification when the 2 long running tasks have completed?
I don't want to use Task.WaitAll, because it will block my UI thread. I don't want to chain the tasks using ContinueWith, because I want those long running tasks to run in parallel.
Use TaskScheduler.FromCurrentSynchronizationContext() and pass it to TaskFactory.ContinueWhenAll(...) to do UI update after both tasks completed.
One way would be to create a third thread that spawns those tasks, and uses Task.WaitAll
to delay thread exit until the two tasks complete. At the end of that thread you can fire an event or execute a callback function.
You could make it even easier by using BackgroundWorker, which has a built-in event called RunWorkerCompleted. This would put you on the thread pool so you don't have to be as concerned with managing that thread.
Try Task.Factory.ContinueWhenAll
. It takes an array of Tasks
and asynchronously fires off an operation when they are all complete.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With