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

I have one form of MDIPrent, which is my main form. Now I exit the Main_Form form by clicking LogOut MenuStrip. In my code, I prevented duplicate instance. But I get this error. I searched so much Google, I did a lot of things, but the error did not disappear. Below is the code for the 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 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 used the context because I want to make Main_Form, the only OpenForm application. Somewhere I got the idea to use context.

+4
source share
3 answers

Your exception is due to the fact that you call Application.Run(...) inside another Application.Run(...) , changing as follows:

 //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. } 
+3
source

try this: instead of starting a new application when you MyApplicationContext() out, just remove your window and replace MyApplicationContext() with this:

  public MyApplicationContext() { bool isSuccessful = false; do { try { lgFrm = new Login_Form(); lgFrm.ShowDialog(); if (lgFrm.LogonSuccessful) { isSuccessful = 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){} }while(isSuccessful); } 
0
source

i had the same problem

the best way to solve this problem is to use

 Application.Restart(); 

you should use it when the user logs out, so the main user interface closes

and the login dialog will appear !!

but with the login form u use this

 ControlBox = False; 

so that the user could not get around it by closing it! Just u can add exit button with code

 Application.Exit(); 

this is how i solved this problem with any additional uses or bugs ...

0
source

Source: https://habr.com/ru/post/1495191/


All Articles