As I recently discovered to my cost, doing an await when there’s no synchronization context may result in the code after the await being executed on a different thread.
I’m currently having problems with strange behaviour in a VSTO office add-in, which I think is possibly a result of this behaviour. When processing events raised by the Office application, there’s no synchronization context in place (unless I create a form, which will create a synchronization context).
My question is whether creating a form is the best / most efficient way to ensure that I have a synchronization context, or whether there’s a simpler way to do it.
Office apps do invoke their events in an STA context, but they do not provide an appropriate
SynchronizationContext.The easiest way to work around this is explained on my blog in
SynchronizationContextOdds and Ends, where I briefly describe a couple of miscellaneous things that I found while doing research for my article but just weren’t important enough to include. To fix this problem, at the beginning of every event, do this:Any
awaits after that should resume on the STA thread.