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:
- in UI thread start a spinner
- in background do some work that needs to be wait
- after that run another background task
- stop the spinner in the UI
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.