Reputation: 10650
I have an asynchronous method from which I update the UI of an WPF UserControl. This WPF User Control is embedded in an ElementHost. This is the way I am able to use an WPF UserControl from a winforms app (Outlook VSTO Add-in). I use an asynchronous method because I need to perform a long task so I avoid blocking UI. I also perform some queries on Outlook objects within a method I call from within the Task. See below.
private async void myMethodAsync()
{
// Show a spinner
this.UpdateUI();
await.Task.Run(() =>
{
// Long task
// Below I perform some queries on Outlook objects.
this.PerformSomeQueriesOnOutlookObjects();
});
}
This is working perfectly, it does not crash nor throw any exception but as I understood you cannot do below actions from an asynchronous method:
So why it is working then? I do not receive any errors.
I was said in the past that I need to go back to the main Outlook UI thread to do these things by using below code. Otherwise when Outlook Object Model (OOM) detects I am trying to perform operations on Outlook objects, it throws an exception (this is not my case).
Dispatcher.CurrentDispatcher.Invoke(() =>
{
// here update UI and access Outlook objects
});
So again, why in my case is working and I do not need to enclose my code within the Invoke and I can even updating UI from the above asynchronous method?
Could someone explain me this?
Upvotes: 0
Views: 49
Reputation: 66276
Does this happen when you run your code under the debugger? Does it fail when you run your code outside of VS?
In the former case, it is possible that the code is actually executed in the VS process and the results are marshaled to the outlook.exe process on its main thread.
Also note that older versions of Outlook do not guard against secondary thread access.
Upvotes: 1