title: How to write simple background thread in CWorkerThread
body: <p>I'm trying to asynchronously run function in my add-on for Internet Explorer (I'm writing BHO in VC++). As suggested <a href="https://stackoverflow.com/questions/4285346/threading-in-bho-atl-com-winapi-or-something-else/4285377#4285377">here</a> I'm trying to use CWorkerThread.</p>
  
  <p>I've been trying to figure it out for hours but still have no idea how to do it. I don't have much experience in ATL. The lack of a good documentations or tutorials on Internet is killing me. </p>
  
  <p>I'm creating class by Add->Class and choosing ATL Simple Object (that's how you add classed to ATL project right?). But how to implement this IWorkerThreadClient? I thought that choosing Add->Implement Interface in Class View would be good but there is no IWorkerThreadClient on the list.</p>
  
  <p>I think I don't know ATL or COM enaugh but can't find good resource for learning this (esspessialy <em>newest</em> ATL7).</p>
  
  <p>I even tried winapi <code>CreateThread</code> approach but it isn't working. I'm passing <code>this</code> class pointer to run static method but something is corrupting with memory later. Nevertheless if It had worked I still would rather use something else than <code>CreateThread</code>.</p>
  
  <p>Right now I have something like <a href="http://msdn.microsoft.com/en-us/library/bb250489%28VS.85%29.aspx" rel="nofollow noreferrer">this</a>. In <code>OnDocumentComplete</code> there's <code>RemoveImages(sptmlDoc)</code> and I just want to run it asynchronously.</p>
  
  <p><strong>EDIT:</strong> What I did with <code>CreateThread</code>:</p>
  
  <p>I tried running RemoveImages function (from <a href="https://stackoverflow.com/questions/4285346/threading-in-bho-atl-com-winapi-or-something-else/4285377#4285377">here</a>) asynchronously. I created static function in my class with signature like <a href="http://msdn.microsoft.com/en-us/library/ms682516%28v=VS.85%29.aspx" rel="nofollow noreferrer">here</a>. RemoveImages has parameter so I copied it to a member of a class:</p>
  
  <pre><code>if (htmlDoc2 != NULL)
  {
      m_tmpHtmlDocument2 = htmlDoc2;
      m_hThread = CreateThread( NULL, 0, MyThreadFunction, this, 0, &amp;m_threadId);
  }
  </code></pre>
  
  <p>and <code>MyThreadFunction</code>:</p>
  
  <pre><code>static DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
  { 
      CHelloWorldBHO* myClass = (CHelloWorldBHO*)lpParam;
      myClass-&gt;RemoveImages(myClass-&gt;m_tmpHtmlDocument2);
  
      return 0; 
  }
  </code></pre>
  
  <p>I get "Unhandled exception at 0x60c0da05 in iexplore.exe: 0xC0000005: Access violation reading location 0x000001b8." here in the bold line:</p>
  
  <pre>
  void CHelloWorldBHO::DontDisplayElement(CComPtr htmlElement)
  {
      CComPtr style;
      HRESULT hr = htmlElement->get_style(&style);
  
      if (hr == S_OK && style != NULL)
      {       
          static const CComBSTR strNone(L"none");
          <b>style->put_display(strNone);</b>
      }
  }
  </pre>