5

我有一份 MDIPrent 表格,这是我的主要表格。现在,我通过单击 LogOut MenuStrip 退出表单 Main_Form。在我的代码中,我防止了重复实例。但我得到这个错误。我用谷歌搜索了很多,尝试了很多东西,但错误并没有消失。以下是 Program.cs 文件的代码:

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){}
            }
        }
}

下面是 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;
    }

下面是 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);
    }

我使用了上下文,因为我想让 Main_Form 成为应用程序的唯一 OpenForm。在某个地方,我有了使用上下文的想法。

4

3 回答 3

4

您的例外是因为您Application.Run(...)在另一个内部调用Application.Run(...),修改如下:

//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.   
 }
于 2013-08-04T14:06:12.023 回答
0

我有同样的问题

解决这个问题的最好方法是使用

Application.Restart();

你应该在用户注销时使用它,所以主 UI 关闭

并出现登录对话框!

但是使用登录表格 U 必须使用这个

ControlBox = False;

所以用户不能通过关闭它来绕过它!只要你可以用代码添加一个退出按钮

Application.Exit();

这就是我在没有任何额外的使用或错误的情况下解决这个问题的方法......

于 2013-11-02T06:22:02.770 回答
0

试试这个:注销时不要触发新应用程序,只需处理您的窗口并替换MyApplicationContext()为:

    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);
    }
于 2013-08-04T13:33:17.663 回答