I've recently changed some code to be asynchronous, using the async/await pattern.
This code is now creating an exception:
private async void Refresh(object stateInfo)
{
await Task.Factory.StartNew(HydrateServerPingDtoList);
// more code here
}
private void HydrateServerPingDtoList()
{
// more code here.
// Exception occurs on this line:
this._serverPingDtoList.Add(new ServerPingDto() { ApplicationServer = server });
}
The exception:
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.
_serverPingDtoList is the backing field for a WPF-bound property. Since I thought that async-await preserved the synchronization context, why would I get this error?
StartNew. What it will do is preserve the context of the generated continuation for// more code here- vcsjonesTask? - Rob Lyndon