我以通常的方式开始我的表单:
Application.Run(new MainForm());
Run Code Online (Sandbox Code Playgroud)
我希望它打开并运行到一定时间,然后关闭.我尝试了以下但无济于事:
(1)在Main方法中(是Application.Run()语句是),我输入以下AFTER Application.Run()
while (DateTime.Now < Configs.EndService) { }
Run Code Online (Sandbox Code Playgroud)
结果:它永远不会被击中.
(2)在Application.Run()之前我启动一个新的后台线程:
var thread = new Thread(() => EndServiceThread()) { IsBackground = true };
thread.Start();
Run Code Online (Sandbox Code Playgroud)
其中EndServiceThread是:
public static void EndServiceThread()
{
while (DateTime.Now < Configs.EndService) { }
Environment.Exit(0);
}
Run Code Online (Sandbox Code Playgroud)
结果:vshost32.exe已停止工作崩溃.
(3)在MainForm Tick事件中:
if (DateTime.Now > Configs.EndService)
{
this.Close();
//Environment.Exit(0);
}
Run Code Online (Sandbox Code Playgroud)
结果:vshost32.exe已停止工作崩溃.
实现目标的正确方法是什么?再次,我想启动表单,让它打开并运行到一定时间(Configs.EndService),然后关闭.
谢谢你,本.