Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead

Tags:

c#

winforms

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:

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 => p.ProcessName == thisprocessname) > 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){}
            }
        }
}

Below is the code for Login_Form

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;
    }

Below is for Main_Form

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);
    }

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.

like image 502
DhavalR Avatar asked Oct 04 '22 08:10

DhavalR


1 Answers

Your exception is because you call Application.Run(...) inside another Application.Run(...), modify as follow:

//MyApplicationContext constructor
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());  <<<---- Remove this
                    MainForm = new Main_Form();
                }
            }
            catch (Exception ex){}
            //Add the ThreadExit event handler here
            ThreadExit += (s,e) => {
              if(Program.loggedOut) {
                Program.MyApplicationContext ctxt = new Program.MyApplicationContext();
                Application.Run(ctxt);
              }
            };
       }
     }
 //
 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();  //I think you want to call Application.Restart() here?
                   //if so, you don't need the ThreadExit event handler added in the MyApplicationContext() constructor.   
 }
like image 113
King King Avatar answered Oct 07 '22 20:10

King King