BackgroundWorker’s RunWorkerCompleted event’s weird behavior in Outlook Add In developed using VSTO 4.0

If one uses “BackgroundWorker” class in an application.Then, as per the documentation and the discussion found online. It is suppose to raise “RunWorkerCompleted” and “ProgressChanged” on the same thread. On which the object of “BackgroundWorker” was created. Which in case of Windows form application would be UI Thread.

But, if one uses “BackgroundWorker” class in an Outlook Add In developed using VSTO 4.0. Then “RunWorkerCompleted” works differently and it is raised on different thread then UI Thread.

One of the possible explanation of this behavior can be found here. What it states that only in case of GUI Windows Forms it is suppose to behave that way. Since, BackgroundWorker uses the synchronization model provided by SynchronizationContext.Current to dispatch events.

In that case the only thing to rescue is InvokeRequired and Invoke . Details of How to use these can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>