title: Problem attaching WatiN to IE
body: <p>I am experimenting with WatiN for our UI testing, I can get tests to work, but I can't get IE to close afterwards. </p>
  
  <p>I'm trying to close IE in my class clean up code, using WatiN's example <a href="http://watinandmore.blogspot.com/2009/03/reusing-ie-instance-in-vs-test.html" rel="nofollow">IEStaticInstanceHelper technique</a>.</p>
  
  <p>The problem seems to be attaching to the IE thread, which times out:</p>
  
  <pre><code>_instance = IE.AttachTo&lt;IE&gt;(Find.By("hwnd", _ieHwnd));
  </code></pre>
  
  <p>(_ieHwnd is the handle to IE stored when IE is first launched.)</p>
  
  <p>This gives the error:</p>
  
  <blockquote>
    <p>Class Cleanup method
    Class1.MyClassCleanup failed. Error
    Message:
    WatiN.Core.Exceptions.BrowserNotFoundException:
    Could not find an IE window matching
    constraint: Attribute 'hwnd' equals
    '1576084'. Search expired after '30'
    seconds.. Stack Trace:     at
    WatiN.Core.Native.InternetExplorer.AttachToIeHelper.Find(Constraint
    findBy, Int32 timeout, Boolean
    waitForComplete)</p>
  </blockquote>
  
  <p>I'm sure I must be missing something obvious, has anyone got any ideas about this one?
  Thanks</p>
  
  <p>For completeness, the static helper looks like this:</p>
  
  <pre><code>public class StaticBrowser
  {
      private IE _instance;
      private int _ieThread;
      private string _ieHwnd;
  
      public IE Instance
      {
          get
          {
              var currentThreadId = GetCurrentThreadId();
              if (currentThreadId != _ieThread)
              {
                  _instance = IE.AttachTo&lt;IE&gt;(Find.By("hwnd", _ieHwnd));
                  _ieThread = currentThreadId;
              }
              return _instance;
          }
          set
          {
              _instance = value;
              _ieHwnd = _instance.hWnd.ToString();
              _ieThread = GetCurrentThreadId();
          }
      }
  
  private int GetCurrentThreadId()
  {
      return Thread.CurrentThread.GetHashCode();
  }
      }
  </code></pre>
  
  <p>And the clean up code looks like this:</p>
  
  <pre><code>private static StaticBrowser _staticBrowser;
  
  [ClassCleanup]
  public static void MyClassCleanup()
  {
      _staticBrowser.Instance.Close();
      _staticBrowser = null;
  }
  </code></pre>