{"id":8857,"date":"2015-03-09T09:50:00","date_gmt":"2015-03-09T16:50:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=8857"},"modified":"2015-03-24T14:34:31","modified_gmt":"2015-03-24T21:34:31","slug":"powershell-studio-knowing-when-to-use-sta-mode","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2015\/03\/09\/powershell-studio-knowing-when-to-use-sta-mode\/","title":{"rendered":"PowerShell Studio: Knowing when to use STA mode"},"content":{"rendered":"<p><a class=\"twitter-follow-button\" href=\"https:\/\/twitter.com\/SAPIENDavid\" data-lang=\"en\">Follow @SAPIENDavid<\/a><\/p>\n<p>Recently, on our forums, a user asked why certain controls didn\u2019t function correctly or threw an error when he unchecked the STA option in PowerShell Studio.<\/p>\n<p style=\"padding-left: 30px\"><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAMode.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8920 size-full\" alt=\"SPS-STAMode\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAMode.png\" width=\"475\" height=\"163\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAMode.png 475w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAMode-300x103.png 300w\" sizes=\"auto, (max-width: 475px) 100vw, 475px\" \/><\/a><\/p>\n<p>STA (Single Threaded Apartment) and MTA (Multi-Threaded Apartment) determine how a process manages legacy COM objects and their communication with multiple threads. I will not go into the detail, but you need not concern yourself with the inner workings of COM objects, other than to remember that STA is required for some of the GUI controls to function correctly.<\/p>\n<p>If you&#8217;re interested in the details, see <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms693344(v=vs.85).aspx\">Processes, Threads, and Apartmen<\/a><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms693344(v=vs.85).aspx\" target=\"_blank\">ts<\/a> in the Microsoft Developer Center.<\/p>\n<h2>Which controls require STA?<\/h2>\n<p>The following controls require STA mode. Otherwise, they do not appear.<\/p>\n<ul>\n<li>FolderBrowserDialog\n<li>SaveFileDialog\n<li>OpenFileDialog <\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The following control features require STA mode.<\/p>\n<ul>\n<li>TextBox with AutoCompleteSource <\/li>\n<li>ComboBox with AutoCompleteSource <\/li>\n<li>DataGridView with Ctrl + C to copy text in cells.\n<li>Form with <em>TopMost<\/em> property set to <em>True<\/em>.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Also, static methods of the Clipboard class require STA mode. For example:<\/p>\n<pre><span style=\"color: #000000\">[<\/span><span style=\"color: #0000cd\">System.Windows.Forms.Clipboard<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #0000ff\">::<\/span><span style=\"color: #000000\">SetText(<\/span><span style=\"color: #ff0000\">\"Paste this text to the clipboard\"<\/span><span style=\"color: #000000\">)<\/span><\/pre>\n<h2><b>Packaging and STA mode:<\/b><\/h2>\n<p>Luckily, PowerShell Studio detects when your form includes an STA mode dependent control and runs it in STA mode. But, when you package a script, be sure to retain the STA mode settings.<\/p>\n<p>Click <b>Deploy<\/b> and in the Packager section, click <b>Settings<\/b> and then <b>Engine Settings<\/b>.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAModePackager.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8921 \" alt=\"SPS-STAModePackager\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAModePackager.png\" width=\"647\" height=\"126\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAModePackager.png 796w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2015\/03\/SPS-STAModePackager-300x58.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Next, make sure that your Windows PowerShell scripts require STA when running. If you add the following comment to your script, PowerShell Studio and PrimalScript run the script in STA mode:<\/p>\n<pre><span style=\"color: #008000\"># %ForceSTA% = True<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Starting in Windows PowerShell 3.0, the Windows PowerShell console runs in STA mode by default. If you are using Windows PowerShell 2.0, use the following command to start Windows PowerShell with the STA option:<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PowerShell.exe \u2013STA<\/p>\n<p>Windows PowerShell ISE always runs in STA mode.<\/p>\n<p>&nbsp;<\/p>\n<p>To determine whether a Windows PowerShell host program is running in STA mode, use this command to check the value of the <em>ApartmentState<\/em> property:<\/p>\n<pre><span style=\"color: #8b0000\">$host<\/span><span style=\"color: #000000\">.Runspace.ApartmentState<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>STA mode is not something you normally have to consider when writing scripts, but it is something that you should be aware of if you are creating GUIs tools. Most GUIs will run fine in MTA mode, but as a general rule, we recommended that you always run your GUI scripts in STA mode.<\/p>\n<p><em><\/em>&nbsp;<\/p>\n<p><em>David Corrales is Senior Software Engineer at SAPIEN Technologies, Inc. F<\/em><em>ollow him on Twitter at <a href=\"https:\/\/twitter.com\/SAPIENDavid\" target=\"_blank\">@SAPIENDavid<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow @SAPIENDavid Recently, on our forums, a user asked why certain controls didn\u2019t function correctly or threw an error when he unchecked the STA option in PowerShell Studio. STA (Single Threaded Apartment) and MTA (Multi-Threaded Apartment) determine how a process manages legacy COM objects and their communication with multiple threads. I will not go into [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[703,644],"tags":[665,41,958,1016,538,997],"class_list":["post-8857","post","type-post","status-publish","format-standard","hentry","category-powershell-studio","category-spotlight-on-controls","tag-form","tag-gui","tag-mta","tag-powershell-studio","tag-sta","tag-windows-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=8857"}],"version-history":[{"count":18,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8857\/revisions"}],"predecessor-version":[{"id":8963,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8857\/revisions\/8963"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=8857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=8857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=8857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}