title: Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead
body: <p>I have one MDIPrent Form that is my main form. Now I am logging out form Main_Form by clicking LogOut MenuStrip. In my code I have prevented duplicate instance. But I get this error. I have googled so much, tried so many things but error doesn't go away.
  Below is code for Program.cs file:</p>
  
  <pre><code>using System.Diagnostics;
  static class Program
  {
      [STAThread]
      static void Main()
      {
          LoggedInUser = string.Empty;
          loginSuccess = false;
          String thisprocessname = Process.GetCurrentProcess().ProcessName;
          if (Process.GetProcesses().Count(p =&gt; p.ProcessName == thisprocessname) &gt; 1)
              return;
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          MyApplicationContext context = new MyApplicationContext();
          Application.Run(context);
  
      }
      public class MyApplicationContext : ApplicationContext
      {
          private Login_Form lgFrm = new Login_Form();
          public MyApplicationContext()
          {
                  try
                  {
                      lgFrm.ShowDialog();
                      if (lgFrm.LogonSuccessful)
                      {
                          ////lgFrm.Close();
                          lgFrm.Dispose();
                          FormCollection frm = Application.OpenForms;
                          try
                          {
                              foreach (Form fc in frm)
                                  fc.Close();
                          }
                          catch (Exception ex){}
                          Application.Run(new Main_Form());
                      }
                  }
                  catch (Exception ex){}
              }
          }
  }
  </code></pre>
  
  <p>Below is the code for Login_Form</p>
  
  <pre><code>public bool LogonSuccessful
      {
          get
          {
              return Program.loginSuccess;
          }
  
          set
          {
              Program.loginSuccess = value;
          }
      }
  
      private void BtnEnter_Click(object sender, EventArgs e)
      {
          Login_Form lgn = new Login_Form();
          Program.loginSuccess = true;
          this.Hide();
          Program.LoggedInUser = TxtBxUserName.Text;
      }
  </code></pre>
  
  <p>Below is for Main_Form</p>
  
  <pre><code>private void LogOutMenuItem_Click(object sender, EventArgs e)
      {
          Login_Form lgFrm = new Login_Form();
          lgFrm.LogonSuccessful = false;
          Program.loggedOut = true;
          Program.LoggedInUser = string.Empty;
          this.Close();
          ////FormCollection frm = Application.OpenForms;
  
          ////foreach (Form fc in frm)
          ////{
          ////    MessageBox.Show(fc.ToString());
          ////}
  
          Program.MyApplicationContext context = new Program.MyApplicationContext();
          Application.Run(context);
      }
  </code></pre>
  
  <p>I have used context, because I want to make Main_Form, the only OpenForm of application. Somewhere I got the idea of using the context.</p>