在我的program.cs文件中,代码如下:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmWizard frm = new frmWizard();
Application.Run(frm);
Thread th = new Thread(frm.ThreadSetCom);
th.Start();
}
ThreadSetCom是一种在无限循环中运行的方法,用于检查某些内容。我注意到ThreadSetCom只会在WinForm出现之前和关闭表单之后执行。表单可见时不执行。有人可以清楚地告诉我吗?
答案 0 :(得分:2)
Application.Run等待,直到参数中传递的表单关闭。你可能需要在frmWizard的create
事件中start
和thread
load
。
private void frmWizard_Load(object sender, System.EventArgs e)
{
Thread th = new Thread(ThreadSetCom);
th.Start();
}
答案 1 :(得分:1)
Application.Run将阻止,直到您关闭表单,该表单解释了您看到此行为的原因。 Adil的答案会奏效,但我相信你不应该以这种方式结合你的代码。如果你的Main方法独立于Form Load事件启动第二个线程会更好。
所以你只需要重新安排你的代码:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmWizard frm = new frmWizard();
Thread th = new Thread(frm.ThreadSetCom);
th.Start();
Application.Run(frm);
}