问题
I'm maintaining a VSTO add-in for Outlook, and it has a BackgroundWorker that handles syncing with a server. I want the worker to trigger user errors in the UI (changing the picture on a button, enabling a message in a settings window).
Does UI manipulation need to be done from the initial thread?
This StackOverflow answer says not, but a comment on it says so. It's for Excel, but still for a VSTO add-in. I tried and it seems to work from either thread, but there could be a race condition I haven't hit yet.
回答1:
Yes, all Outlook / Office Object Model access must be done from the main thread. See VSTO with Windows Form and Worker Threads
回答2:
I completely agree with Dmitry. Office applications use the single threaded apartments. You should access the Object model on the main thread only. If you need to do some background processing you may extract the required information from Outlook objects and run a secondary thread for processing the data on the secondary thread. Or you may use a low-level API - Extended MAPI (or any other third-party wrappers around that API).
来源:https://stackoverflow.com/questions/29931477/vsto-outlook-add-in-does-ui-manipulation-need-to-be-done-from-main-thread