title: Do i need to call CoInitialize before interacting with COM in .NET?
body: <p>i know that a <strong>requirement</strong> of COM that every thread call <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms678543%28v=vs.85%29.aspx"><code>CoInitialize</code></a> before interacting with the COM system. </p>
  
  <p>.NET exposes some items that internally operate on threads, e.g.:</p>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx"><code>ThreadPool</code></a> threads</li>
  <li><a href="http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx">asychronous delegates</a> (which use thread pool threads)</li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx"><code>BackgroundWorker</code></a> class (which use asychronous delegates (which use thread pool threads))</li>
  <li>the <a href="http://msdn.microsoft.com/en-us/library/yhwwzef8.aspx">garbage collector</a></li>
  <li><em>and more! (i.e. e.g.)</em></li>
  </ul>
  
  <p>If i am going to be interacting with a COM object from a thread, do i need to call <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms678543%28v=vs.85%29.aspx"><code>CoInitialize</code></a> first?</p>
  
  <p>i ask because there may be some <a href="http://msdn.microsoft.com/en-us/library/system.stathreadattribute.aspx">more magic that automagically calls it for me</a> - i don't know.</p>
  
  <hr>
  
  <h2>Bonus Reading</h2>
  
  <blockquote>
    <h2><a href="http://msdn.microsoft.com/en-us/library/5s8ee185.aspx">Managed and Unmanaged Threading</a></h2>
    
    <p>For interoperability, the common language runtime creates and
    initializes an apartment when calling a COM object. A managed thread
    can create and enter a single-threaded apartment (STA) that contains
    only one thread, or a multi-threaded apartment (MTA) that contains one
    or more threads. When a COM apartment and a thread-generated apartment
    are compatible, COM allows the calling thread to make calls directly
    to the COM object. If the apartments are incompatible, COM creates a
    compatible apartment and marshals all calls through a proxy in the new
    apartment.</p>
    
    <p>The runtime calls CoInitializeEx to initialize the COM apartment as
    either an MTA or an STA apartment.</p>
  </blockquote>
  
  <p><strong>Update Two:</strong></p>
  
  <p>Looks like you should not use COM from any kind of thread that .NET can provide:</p>
  
  <blockquote>
    <h2><a href="http://msdn.microsoft.com/en-us/library/0ka9477y.aspx">The Managed Thread Pool</a></h2>
    
    <p>There are several scenarios in which it is appropriate to create and
    manage your own threads instead of using thread pool threads:</p>
    
    <ul>
    <li><p>You require a foreground thread.</p></li>
    <li><p>You require a thread to have a particular priority.</p></li>
    <li><p>You have tasks that cause the thread to block for long periods of
    time. The thread pool has a maximum number of threads, so a large
    number of blocked thread pool threads might prevent tasks from
    starting.</p></li>
    <li><p><em><strong>You need to place threads into a single-threaded apartment. All
    ThreadPool threads are in the multithreaded apartment.</em></strong></p></li>
    <li><p>You need to have a stable identity associated with the thread, or to
    dedicate a thread to a task.</p></li>
    </ul>
  </blockquote>
  
  <p><strong>Update Three</strong>:</p>
  
  <p>Looks like you <strong><em>can</em></strong> set the threading model of unmanged threads:</p>
  
  <blockquote>
    <h2><a href="http://msdn.microsoft.com/en-us/library/74169f59.aspx">Managed and Unmanaged Threading in Microsoft Windows</a></h2>
    
    <p>A managed thread can be marked to indicate that it will host a single-threaded or multithreaded apartment. The <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.getapartmentstate.aspx">GetApartmentState</a>, <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate.aspx">SetApartmentState</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.trysetapartmentstate.aspx">TrySetApartmentState</a> methods of the <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx">Thread</a> class return and assign the apartment state of a thread. If the state has not been set, <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.getapartmentstate.aspx">GetApartmentState</a> returns <a href="http://msdn.microsoft.com/en-us/library/system.threading.apartmentstate.aspx">ApartmentState.Unknown</a>.</p>
    
    <p>The property can be set only when the thread is in the <a href="http://msdn.microsoft.com/en-us/library/system.threading.threadstate.aspx">ThreadState.Unstarted</a> state; it can be set only once for a thread.</p>
    
    <p>If the apartment state is not set before the thread is started, the thread is initialized as a multithreaded apartment (MTA). </p>
  </blockquote>
  
  <p>Lot of conflicting information.</p>
  
  <p>Which is why we'll use whatever the guy on Stackoverflow said as the true answer.</p>