Project Description
 
Eric De Carufel
This project is used to demonstrate a bug in .NET 4.0 Runtime for Task Factory.

The problem

I found a problem in .NET 4.0 with Task.Factory.FromAsync. Here is the scenario.

I have a wpf application with two ListBox. Their ItemsSource property is bound to ObservableCollection and their SelectedItem property to a property in the view model with a TwoWay binding. The two list boxes have a master/detail relationship. So, if the selection on the first listbox change it triggers a reload of the second one by changing the selected item.

Both list box are loaded via a wcf service call made with a Task.Factory.FromAsync method. When the first list box is loaded it automatically select its first item and that triggers the second list box to load. Both service call are made in background and the result is added in the list box with a ContinueWith call with TaskScheduler.FromCurrentSynchronizationContext call. There is a problem, in .NET 4.0 the second call fails with a System.InvalidOprationException (The current SynchronizationContext may not be used as a TaskScheduler.) exception. If you install .NET 4.5 Runtime it will fix the problem.

TaskFactoryBugSample.png

Last edited Oct 22, 2013 at 11:07 AM by decarufe, version 9