In my WPF application I do some async communication (with server). In the callback function I end up creating InkPresenter objects from the result from server. This requires the running thread to be STA, which apparently it currently isn't. Therefore I get the following exception:
Cannot create instance of 'InkPresenter' defined in assembly [..] The calling thread must be STA, because many UI components require this.
Currently my async function call is like this:
public void SearchForFooAsync(string searchString)
{
var caller = new Func<string, Foo>(_patientProxy.SearchForFoo);
caller.BeginInvoke(searchString, new AsyncCallback(SearchForFooCallbackMethod), null);
}
How can I make the callback - which will do the InkPresenter creation - be STA? Or invoke the XamlReader parsing in a new STA thread.
public void SearchForFooCallbackMethod(IAsyncResult ar)
{
var foo = GetFooFromAsyncResult(ar);
var inkPresenter = XamlReader.Parse(foo.Xaml) as InkPresenter; // <!-- Requires STA
[..]
}
You can use the Dipatcher class to execute the method call on the UI-Thread. The Dispatcher provides the static property CurrentDispatcher to get the dispatcher of a thread.
If your object of the class, that creates the InkPresenter, is created on the UI-Thread, then the CurrentDispatcher method returns the Dispatcher of the UI-Thread.
On the Dispatcher you can call the BeginInvoke-method to call the specified delegate asynchronously on the thread.
It should be good enough to call it on the UI thread. Therefore, use a
BackgroundWorker
and on theRunWorkerAsyncCompleted
, you can then do the creation of the inkPresenter.You can start STA Threads like so:
The only problem is that your result object must be passed along somehow.. You can use a private field for that, or dive into passing along parameters into threads. Here I set the foo data in a private field and start up the STA Thread to mutate the inkpresenter!
Hope this helps!
It's a bit of a hack, but I would use XTATestRunner So your code will look like:
as a bonus it's possible to catch exceptions thrown in STA (or MTA) thread like this: