在单个线程上启动第二个消息循环不是有效的操作.请改用Form.ShowDialog

Dha*_*alR 5 c# winforms

我有一个MDIPrent表格,这是我的主要表格.现在我通过单击LogOut MenuStrip登出表单Main_Form.在我的代码中,我已经阻止了重复实例.但是我得到了这个错误.我用Google搜索了很多东西,尝试了很多东西,但错误并没有消失.下面是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){}
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

下面是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;
    }
Run Code Online (Sandbox Code Playgroud)

以下是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);
    }
Run Code Online (Sandbox Code Playgroud)

我使用了上下文,因为我想制作Main_Form,它是应用程序的唯一OpenForm.在某个地方,我有了使用上下文的想法.

No *_*ame 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);
    }
Run Code Online (Sandbox Code Playgroud)