title: BeginInvoke fails because a window handle has not been created
body: <p>I am maintaining a program with customer informations. It consists of many forms that each show some relevant info from the database. This error is in a single form after doing the following</p>
  
  <ol>
  <li>Open the customer search form</li>
  <li>View random customer A info in the customerinfo form</li>
  <li>open the crm form and it automatically shows customer A. Then add a file to him via draganddrop.</li>
  <li>Close the last two forms and select random customer B and do the same.</li>
  <li>Close the last two forms and select customer A and add a new file. Error!!!</li>
  </ol>
  
  <p>Here is the code that fails:</p>
  
  <pre><code>private void FireFileCountChanged() {
      if (FileCountChanged != null)
          BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2); // FAILS
  </code></pre>
  
  <p>"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll</p>
  
  <p>Additional information: Invoke or BeginInvoke cannot be called on a control until the window handle has been created."</p>
  
  <p>I tried adding the following:</p>
  
  <pre><code>private void FireFileCountChanged() {
          if (FileCountChanged != null &amp;&amp; this.Handle != null) // CHANGED AND FAILS.
              BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
      }
  </code></pre>
  
  <p>But the this.handle gives:</p>
  
  <p>'this.Handle' threw an exception of type 'System.ObjectDisposedException' and
  "Cannot access a disposed object.\r\nObject name: 'AttachmentsControl'."</p>
  
  <p>Then I added a timeout of 10 seconds as the first line in the method, but the handle is still not created. Has the handle somehow been disposed when one of the window were closed? And what can be done about this? Any help is appreciated. I'm kind of stuck.</p>
  
  <pre><code>private void FireFileCountChangedDeferred(int repostCount) {
      if (FileCountChanged != null) {
          if (repostCount &gt; 0) {
              //black magic is somehow involved in getting this event to fire *after* the filewatcher reports the change.
              System.Threading.Thread.Sleep(10);
              BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), repostCount - 1);
          } else
              FileCountChanged(this, null);
      }
  }
  
  private void CopyFiles(string[] files, bool reload) {
      if (CreatePath()) {
          foreach (string src in files) {
              try {
                  string dest = MakeSafeFilename(src);
                  File.Copy(src, dest);
                  FireFileCountChanged();
              } catch (Exception ex) {
                  //Util.Print("Copy ex: {0}", ex.Message);
                  ErrMsg("Error while copying:{1}{0}", ex.Message, environment.NewLine);
              }
          }
      }
  }
  
  private void Lstv_DragDrop(object sender, DragEventArgs ea) {
      if (m_CanAdd) {
          string[] files = GetDraggedFiles(ea);
          if (files != null)
              CopyFiles(files, true);
          else if (OutlookDataObject.HoldsOutlookData(ea) &amp;&amp; CreatePath()) {
              try {
                  OutlookDataObject.CopyDroppedFiles(ea, m_Path, OutlookFilenameCallback);
              } catch (Exception ex) {
                  //Util.Print("Copy ex: {0}", ex.Message);
                  ErrMsg("Error copying from Outlook:{1}{0}", ex.Message, Environment.NewLine);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <pre><code>private void FireFileCountChanged() {
                  while (!this.IsHandleCreated) // added
                            System.Threading.Thread.Sleep(100); //added
  
      if (FileCountChanged != null)
          BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
  </code></pre>