Search code examples
c#windowsshowdialog

How can I run a second application from another one (C#)


Can you tell me how can I close a first application and immediately run a second application?

First Step: (Login validation)

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Login());
}

Second Step: (Run main program)

If the user succesfully logins to program, I need to close the login application and run the new application named "Main".

The check for the login is the following:

if (access.access == true)
{
    Application.Run(new Main());
    Close();
}
else
    MessageBox.Show("Přihlašovací jméno nebo heslo neni správné");

Debug Error:

Create a second message loop on a single thread is an invalid operation. Instead, use the Application.RunDialog or form.ShowDialog.

I think that the best answer for my problem is to use ShowDialog and DialogResult, but I don't know how can I use them for my settings.


Solution

  • You could run a modal dialog asking for credentials before entering the main loop

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
    
        bool validated = false;
        using(frmLogin fLogin = new frmLogin())
        {
             if(fLogin.ShowDialog() == DialogResult.OK)
                 validated = true;
        }
        if(validated)
            Application.Run(new Main());
        else
            MessageBox.Show("Bye");
    }