Here is a small example of my problem, and I havent been able to solve it from the other questions about the same error.
UI Thread calls a function from a button click that has the following line:
await DataSet.AddFileAsync(String.Format("ms-appdata:///local/IMM_FACE_DB/{0}", file.Name));
AddFileAsync looks like this:
public ObservableCollection<Model> Data {get;set;}
public Task AddFileAsync(string path)
{
return Task.Factory.StartNew((state) =>
{
// Here some IO and computation is done, eventually some content is added
// to Data. Data is also bound to a GridView and this fails.
//I assumed that TaskScheduler.FromCurrentSynchronizationContext()
// would solve this, but it does not.
}, TaskScheduler.FromCurrentSynchronizationContext());
}
As i have written in the code above, I assumed that the TaskScheduler could make sure it was run on the UI thread. Since the AddFileAsync is called from Databound Buttonclick Command.
Where have i misunderstood something? and what would be the correct wait to do this.
There’s no need for
StartNewthat I can see…