Question

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.

Était-ce utile?

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

Autres conseils

Have a look at Creating a Windows Forms Application With Login

Here the difference is that the Main still runs frmMain

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

The main form handles the frmLogin

public partial class frmMain : Form 
{ 
    frmLogin _login = new frmLogin(); 


    public frmMain() 
    { 
        InitializeComponent(); 
        _login.ShowDialog(); 
        if (_login.Authenticated) 
        { 
            MessageBox.Show("You have logged in successfully " + _login.Username); 
        } 
        else 
        { 
            MessageBox.Show("You failed to login or register - bye bye","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
            Application.Exit(); 
        } 
    } 
} 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top