Reputation: 624
I using the Parallel Task library to run async tasks in the background. Sometimes I have to wait that background task's completed delegate while the UI thread is non-blocked.
So:
I started the background task in the UI thread (2) after that I tried to call Wait method, which is cause a deadlock...
I need a solution to wait a specified background task without blocking the UI thread. .NET framework is 4 so I can't use async
and await
Update:
In the UI thread:
Task task = Task.Factory.StartNew<T>(() => BackgroundTask());
task.ContinueWith(task => CompletedTask(((Task<T>)task).Result); // I want to prevent later tasks to start before this task is finished
task.Wait(); // this will block the ui...
Any advice appreciated.
Upvotes: 0
Views: 1310
Reputation: 203812
You use ContinueWith
to attach a continuation to the task and run some code when the task finishes. await
is doing effectively the same thing, behind the scenes.
You can use Task.Factory.ContinueWhenAll
to run a continuation when all of a collection of tasks have finished.
You can use TaskScheduler.FromCurrentSynchronizationContext()
from within the UI thread to get a task scheduler capable of scheduling tasks in the UI thread.
Upvotes: 1