I am using Task.Factory.StartNew to make the long processing service calls off the UI thread.However, i am getting the follwing exception as soon as i load the application,
A task’s exception were not observed either by waiting on the Task or accessing its Exception property. As a result the unhandled exception was rethrown by the finalizer thread.
Task.Factory.StartNew(() => this.InitializeViewModel(myViewModel));
private void InitializeViewModel(IModel myViewModel)
{
lock (viewModelLock)
{
myViewModel.MyContext = this.MyContext; // this will set the MyContext property which in turn makes some service calss
}
}
here i can’t use task.wait() b’coz then it will cause the wait on the UI thread..
how can i avoid this exception??
Thanks.
as a first workaround/debugging-help wrap your
lock (...)inside atry-catchand set a breakpoint inside thecatch-Block – this way you should be able to see the problemTo the problem with the
Task.Wait– you can use the ContinueWith method to get the exceptions:don’t forget the handler: