gpt4 book ai didi

c# - 在单个线程上启动第二个消息循环不是有效操作。改为使用 Form.ShowDialog

转载 作者:太空狗 更新时间:2023-10-29 23:23:23 25 4
gpt4 key购买 nike

我有一个 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);
}

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

最佳答案

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

关于c# - 在单个线程上启动第二个消息循环不是有效操作。改为使用 Form.ShowDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18043190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com