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) {
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.
source share