I have a method that I want to execute in the background using Task. However, the method requires an object reference as a parameter. However, the object is created in the user interface thread, so I got "the calling thread cannot access this object because another thread belongs to it." an exception. How am I supposed to do this?
Task.Factory.StartNew(() => SerializeGraphicsLayer(graphicsLayer, fileUrl)) .ContinueWith((t) => UpdateSaveOperation(t), TaskScheduler.FromCurrentSynchronizationContext());
The SerializeGraphicsLayer () method is the one I want to use in the background, however I need to pass a link to the object created in the UI thread to this method ...
EDIT: The method (which works in backgound) may throw an exception, and the UpdateSaveOperation () method will make the necessary error message in the user interface.
I would like to have a good exception handling, and that is why I chose Task for this?
icube source share