Thursday 24 October 2013

Don't Block the UI Thread with Task.WaitAll use TaskFactory.ContinueWhenAll

The WPF MVVM application I've been working on recently had a view model which loaded data from a couple of different sources. Like all good developers I didn't want to block the UI thread and have an unresponsive app so I did this.

 Task[] tasks = new[]  
 {  
   _resourcesService.LoadSomething(result => { /* Handle callback for this method*/ }),  
   _otherService.GetSomethingElse(result => { /* Handle callback for this method*/ })  
 };  
 Task.Factory.ContinueWhenAll(tasks, t =>  
 {  
   // Check for faulted tasks.  
   // Do tasks that can only be handled when everything is loaded  
 });