{"id":283,"date":"2014-08-11T07:00:00","date_gmt":"2014-08-11T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/08\/11\/if-you-want-to-set-a-threads-apartment-model-via-thread-currentthread-apartmentstate-you-need-to-act-quickly\/"},"modified":"2014-08-11T07:00:00","modified_gmt":"2014-08-11T07:00:00","slug":"if-you-want-to-set-a-threads-apartment-model-via-thread-currentthread-apartmentstate-you-need-to-act-quickly","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140811-00\/?p=283","title":{"rendered":"If you want to set a thread&#039;s apartment model via Thread.CurrentThread.ApartmentState, you need to act quickly"},"content":{"rendered":"<p>\nWelcome to CLR Week 2014.\nDon&#8217;t worry, it&#8217;ll be over in a few days.\n<\/p>\n<p>\nA customer wanted to know why their\n<code>Folder&shy;Browser&shy;Dialog<\/code>\nwas displaying the infamous\n<i>Current thread must be set to single thread apartment (STA) mode\nbefore OLE calls can be made<\/i> error.\n<\/p>\n<pre>\nprivate void btnBrowseFolder_Click(object sender, System.EventArgs e)\n{\n  Thread.CurrentThread.ApartmentState = ApartmentState.STA;\n  FolderBrowserDialog fbd = new FolderBrowserDialog {\n    RootFolder = System.Environment.SpecialFolder.MyComputer,\n    ShowNewFolderButton = true,\n    Description = \"Select the awesome folder...\"\n  };\n  DialogResult dr = fbd.ShowDialog();\n  ...\n}\n<\/pre>\n<p>\n&#8220;Even though we set the\n<code>Apartment&shy;State<\/code> to <code>STA<\/code>,\nthe apartment state is still <code>MTA<\/code>.\nCuriously, if we put the above code in a standalone\ntest program, it works fine.&#8221;\n<\/p>\n<p>\nThe problem is that the customer is changing the apartment\nstate too late.\n<\/p>\n<blockquote CLASS=\"q\"><p>\nOn the first call to unmanaged code,\nthe runtime calls <b>Co&shy;Initialize&shy;Ex<\/b>\nto initialize the COM apartment as either an MTA or an STA apartment.\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/5s8ee185%28v=vs.71%29.aspx\">\nYou can control the type of apartment created<\/a>\nby setting the\nSystem.Threading.ApartmentState property on the thread to <b>MTA<\/b>,\n<b>STA<\/b>, or <b>Unknown<\/b>.\n<\/p><\/blockquote>\n<p>\nNotice that the value you specify in\n<code>Current&shy;Thread.Apartment&shy;State<\/code>\nis consulted at the point the runtime initializes the COM apartment\n(which occurs on the first call to unmanaged code).\nIf you change it after the COM apartment has been initialized,\nyou&#8217;re revising the blueprints of a house after it has been built.\n<\/p>\n<p>\nThe standard way to avoid this problem is to attach the\n<code>[STAThread]<\/code> attribute to your <code>Main<\/code>\nfunction,\nor if you need to set the apartment model of a thread\nyou created yourself,\ncall the\n<code>Thread.Set&shy;Apartment&shy;State<\/code> method\nbefore the thread starts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to CLR Week 2014. Don&#8217;t worry, it&#8217;ll be over in a few days. A customer wanted to know why their Folder&shy;Browser&shy;Dialog was displaying the infamous Current thread must be set to single thread apartment (STA) mode before OLE calls can be made error. private void btnBrowseFolder_Click(object sender, System.EventArgs e) { Thread.CurrentThread.ApartmentState = ApartmentState.STA; FolderBrowserDialog [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Welcome to CLR Week 2014. Don&#8217;t worry, it&#8217;ll be over in a few days. A customer wanted to know why their Folder&shy;Browser&shy;Dialog was displaying the infamous Current thread must be set to single thread apartment (STA) mode before OLE calls can be made error. private void btnBrowseFolder_Click(object sender, System.EventArgs e) { Thread.CurrentThread.ApartmentState = ApartmentState.STA; FolderBrowserDialog [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}