This method call sits inside a class derived from DispatcherObject
:
Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() =>
{
var exported = formatProvider.Export(original.Workbook);
Workbook = formatProvider.Import(exported);
}));
The method on the class is called by a backgroundworker in its DoWork
delegate.
Workbook is Telerik's Workbook, as used by the RadSpreadsheetControl. Obviously, workbooks can only be accessed by the UI thread.
The above code throws an InvalidOperationException
, saying
The calling thread must be STA, because many UI components require this.
I don't really understand, as I thought that when invoking the actions with a Dispatcher, I would be calling it from the UI Thread, which is STA?
What am I missing here and how can this be fixed? Or should this work in general and the bug is somewhere else? What could be a reason then?
TL;DR: You must create this
DispatcherObject
inside your UI thread, not in a worker.DispatcherObject.Dispatcher
, which you are marshalling the operation to, is set toDispatcher.CurrentDispatcher
at the time of the object's construction. If the object is not created inside your existing UI thread then the documented behavior ofCurrentDispatcher
is to create a new dispatcher object associated with the thread. Later on,Invoke
tries to marshal the call to that thread (which is not STA) resulting in the error.