title: Close Form from another thread
body: <p>I have a manager class which launch a Form by using the ShowDialog function. Now, I'm starting an event (like a timer) and would like the manager to close the Form while the timer expire.</p>
  
  <p>I used 2 classes:</p>
  
  <pre><code>namespace ConsoleApplication3
  {
  class Manager
  {
      Timer UpdTimer = null;
      readonly int REFRESH_STATUS_TIME_INTERVAL = 5000;
      Form1 form1;
  
      public Manager()
      {
      }
  
      public void ManageTheForms()
      {
          UpdTimer = new Timer(REFRESH_STATUS_TIME_INTERVAL);
          // start updating timer
          //UpdTimer.Interval = REFRESH_STATUS_TIME_INTERVAL;
          UpdTimer.Elapsed += new ElapsedEventHandler(PriorityUpdTimer_Elapsed);
          UpdTimer.Start();
  
          form1 = new Form1();
          form1.ShowDialog();
  
  
      }
  
      public void PriorityUpdTimer_Elapsed(object source, ElapsedEventArgs e)
      {
          UpdTimer = null;
          form1.closeFormFromAnotherThread();
  
      }
  }
  }
  </code></pre>
  
  <p>Form1 Class:</p>
  
  <pre><code>namespace ConsoleApplication3
  {
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {
  
      }
  
  
      delegate void CloseFormFromAnotherThread();
  
      public void closeFormFromAnotherThread()
      {
          if (this.InvokeRequired)
          {
              CloseFormFromAnotherThread del = new CloseFormFromAnotherThread(closeFormFromAnotherThread);
              this.Invoke(del, new object[] { });
          }
          else
          {
              this.Close();
          }
      }
  
  }
  </code></pre>
  
  <p>}</p>