Arunachalam
Arunachalam

Reputation: 6037

how to handle this exception :Invalidoperation exception

Application.Run(form);

Actually I tried to call this from my project. I got this exception. How to solve it? I have already called another Application.Run(frmBind); in my project.

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

 static void Main(string[] args)
 {
    frmBind = new frmMain();

    Application.Run(frmBind);

    //args1 = string.Copy(args);
}

This is where I call the Application at first

Now again did it here:

try
{
   // Application.Run( form);
   form.ShowDialog();
}

Here the exception is thrown.

Upvotes: 2

Views: 6858

Answers (2)

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

You can only call Application.Run once in a thread. Application.Run will (amongst other things) set up the main message loop for the thread, and there can be only one such loop. This is why you get the exception.

If you simply want to display a form, just use form.Show() or form.ShowDialog() instead. Note that calling ShowDialog for a form that is already visible will throw an InvalidOperationException as well (but with another message).

Upvotes: 4

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421968

It's telling you how to solve it:

 form.Show();  

or if you want the new form to be modal:

 form.ShowDialog();

Upvotes: 7

Related Questions