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

本文关键字:操作 有效 ShowDialog Form 线程 单个 启动 第二个 循环 消息 | 更新日期: 2023-09-27 18:26:32

我有一个MDIPrent Form,它是我的主窗体。现在我通过单击注销菜单条来注销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;
    }

以下是主要表单

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。在某个地方,我萌生了使用上下文的想法。

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

您的异常是因为您在另一个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.   
 }

试试这个:与其在注销时启动新的应用程序,不如处理您的窗口并将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);
    }

我遇到了同样的问题

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

Application.Restart();

你应该在用户注销时使用它,这样主UI就会关闭

然后出现登录对话框!!

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

ControlBox = False;

所以用户无法通过关闭它来绕过它!!只需U就可以添加一个代码为的退出按钮

Application.Exit();

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