{"id":4416,"date":"2014-03-30T10:45:17","date_gmt":"2014-03-30T10:45:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/thread-safety-using-ocxs-from-c-net-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:45:17","modified_gmt":"2014-03-30T10:45:17","slug":"thread-safety-using-ocxs-from-c-net-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/thread-safety-using-ocxs-from-c-net-collection-of-common-programming-errors\/","title":{"rendered":"Thread safety using OCXs from C# .NET-Collection of common programming errors"},"content":{"rendered":"<p>I haven&#8217;t used OLE\/COM for quite some time as a developer, but I currently have a need to use some 3rd party OCX code libraries from a C# program.<\/p>\n<p>The C# program uses threading (it&#8217;s a TCP socket server). The OCXs are marked as Apartment threading model. From my reading, I concluded that if I was careful to create one instance of each OCX per thread, and only to use that instance from the thread that created it, I should be OK.<\/p>\n<p>I did also do:-<\/p>\n<pre><code>myThread.SetApartmentState(ApartmentState.STA);\n<\/code><\/pre>\n<p>before starting each thread.<\/p>\n<p>Should this be enough to ensure safe use of the OCXs?<\/p>\n<p>The symptom I&#8217;m seeing is that threads can all create OCXs but on an apparently random basis, the calls to prepare and initialize the OCXs fail. They don&#8217;t seem to return any useful information as to why.<\/p>\n<p>Can anyone explain what I&#8217;m seeing, or give me a guide to using these OCXs safely from threaded code?<\/p>\n<p>Alternatively, should I just give up and create a single instance of each and all OCXs in one thread, and send all calls to them via a threadsafe queue or similar?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I haven&#8217;t used OLE\/COM for quite some time as a developer, but I currently have a need to use some 3rd party OCX code libraries from a C# program. The C# program uses threading (it&#8217;s a TCP socket server). The OCXs are marked as Apartment threading model. From my reading, I concluded that if I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4416","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4416"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4416\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}