{"id":613,"date":"2011-07-19T13:29:00","date_gmt":"2011-07-19T13:29:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/buckh\/2011\/07\/19\/knowing-which-thread-backgroundworker-will-use-for-its-events\/"},"modified":"2011-07-19T13:29:00","modified_gmt":"2011-07-19T13:29:00","slug":"knowing-which-thread-backgroundworker-will-use-for-its-events","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/buckh\/knowing-which-thread-backgroundworker-will-use-for-its-events\/","title":{"rendered":"Knowing which thread BackgroundWorker will use for its events"},"content":{"rendered":"<p>[UPDATE 7\/19\/2011] &nbsp;Stephen pointed me to his article covering this and more in February issue of MSDN Magazine, and I recommend it:&nbsp;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/magazine\/gg598924.aspx\">http:\/\/msdn.microsoft.com\/en-us\/magazine\/gg598924.aspx<\/a>.<\/p>\n<p>We hit this recently, so I thought I&rsquo;d post this email from Chad, a developer on version control, for anyone else who may have missed this subtlety.<\/p>\n<blockquote>\n<p>Today we discovered some of our code was making an incorrect assumption about the behavior of BackgroundWorker, so I thought it might be useful to send a note detailing what we found.<\/p>\n<p>Our code assumed BackgroundWorker would always call ProgressChanged and RunWorkerCompleted on the UI thread.&nbsp; This mistake was based on the assumption that BackgroundWorker saved off the SynchronizationContext for the thread on which it was created.<\/p>\n<p>After reviewing the BackgroundWorker code, we found that it actually saves the SynchronizationContext for the thread where RunWorkerAsync is called (by calling AsyncOperationManager.CreateOperation).&nbsp; Then, ProgressChanged and RunWorkerCompleted are called on that thread if it is still running.&nbsp; If not, the events appear to be called on a random thread.<\/p>\n<p>This of course leads to a crash when there are attempts to update UI from the wrong thread.&nbsp; If you are relying on BackgroundWorker to return you to the UI thread, make sure to only call RunWorkerAsync from the UI thread.<\/p>\n<p>Hope this helps!<\/p>\n<p>-Chad<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>[UPDATE 7\/19\/2011] &nbsp;Stephen pointed me to his article covering this and more in February issue of MSDN Magazine, and I recommend it:&nbsp;http:\/\/msdn.microsoft.com\/en-us\/magazine\/gg598924.aspx. We hit this recently, so I thought I&rsquo;d post this email from Chad, a developer on version control, for anyone else who may have missed this subtlety. Today we discovered some of our [&hellip;]<\/p>\n","protected":false},"author":94,"featured_media":10268,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-c"],"acf":[],"blog_post_summary":"<p>[UPDATE 7\/19\/2011] &nbsp;Stephen pointed me to his article covering this and more in February issue of MSDN Magazine, and I recommend it:&nbsp;http:\/\/msdn.microsoft.com\/en-us\/magazine\/gg598924.aspx. We hit this recently, so I thought I&rsquo;d post this email from Chad, a developer on version control, for anyone else who may have missed this subtlety. Today we discovered some of our [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/users\/94"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/comments?post=613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/posts\/613\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media\/10268"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/categories?post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/buckh\/wp-json\/wp\/v2\/tags?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}