title: Find the open forms in c# windows application
body: <p>I am using this function to close existing form and open a new form.</p>
  
  <p>If there is no exixting form, it throws error.</p>
  
  <p>Error :</p>
  
  <p>Target  :  System.Object MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)</p>
  
  <p>Message :  Invoke or BeginInvoke cannot be called on a control until the window handle has been created.</p>
  
  <p>Stack   :     at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)</p>
  
  <p>SO need to check for any form open before closing the form to avoid the error. How?</p>
  
  <pre><code>    static public void NewMainForm(Form main, bool ClosePreviousMain)
      {
              if (main != null)
              {
                  Global.ActiveForm = main.Text;
                  if (ClosePreviousMain &amp; MyContext.curMain != null)
                  {
                      MyContext.curMain.FormClosed -= new FormClosedEventHandler(main_FormClosed);
                      //Need to check for any form active and then close the form.
                      MyContext.curMain.Invoke(new Action(MyContext.curMain.Dispose));
                  }
                  MyContext.curMain = main;
                  MyContext.curMain.FormClosed += new FormClosedEventHandler(main_FormClosed);
                  MyContext.curMain.ShowDialog();
              }
      }
  </code></pre>