C# 在一段时间后以编程方式关闭 WinForms 应用程序的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15490915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the proper way to programmatically close a WinForms application after a certain time?
提问by Ben
I start my form in the usual way:
我以通常的方式开始我的表格:
Application.Run(new MainForm());
I want it to open and run until a certain time, then close. I've tried the following but to no avail:
我希望它打开并运行到某个时间,然后关闭。我尝试了以下但无济于事:
(1) In the Main method (were the Application.Run() statement is), I enter the following AFTER Application.Run()
(1) 在 Main 方法中(是 Application.Run() 语句),我在 Application.Run() 之后输入以下内容
while (DateTime.Now < Configs.EndService) { }
RESULT: It never gets hit.
结果:它永远不会被击中。
(2) BEFORE Application.Run() I start a new Background Thread:
(2) BEFORE Application.Run() 我开始一个新的后台线程:
var thread = new Thread(() => EndServiceThread()) { IsBackground = true };
thread.Start();
where EndServiceThread is:
其中 EndServiceThread 是:
public static void EndServiceThread()
{
while (DateTime.Now < Configs.EndService) { }
Environment.Exit(0);
}
RESULT: vshost32.exe has stopped working crash.
结果:vshost32.exe 已停止工作崩溃。
(3) In MainForm Tick Event:
(3) 在 MainForm Tick 事件中:
if (DateTime.Now > Configs.EndService)
{
this.Close();
//Environment.Exit(0);
}
RESULT: vshost32.exe has stopped working crash.
结果:vshost32.exe 已停止工作崩溃。
What is the proper way to achieve my goal? Again, I want to launch the Form, have it open & running until a certain time (Configs.EndService), then close.
实现我的目标的正确方法是什么?同样,我想启动表单,让它打开并运行到某个时间 (Configs.EndService),然后关闭。
Thank you, Ben.
谢谢你,本。
采纳答案by Jim Mischel
Create a Timer, and have it close the program in its event handler.
创建一个Timer,并让它在其事件处理程序中关闭程序。
Say you want the application to shut down after 10 minutes. You initialize the timer with a period of 60,000 milliseconds. Your event handler becomes:
假设您希望应用程序在 10 分钟后关闭。您以 60,000 毫秒的周期初始化计时器。您的事件处理程序变为:
void TimerTick(object sender)
{
this.Close();
}
If you want it to close at a specific date and time, you can have the timer tick once per second, and check DateTime.Now
against the desired end time.
如果您希望它在特定日期和时间关闭,您可以让计时器每秒打勾一次,并检查DateTime.Now
所需的结束时间。
This will work because the TimerTick
will execute on the UI thread. The problem with your separate thread idea was that Form.Close
was called on a background thread, notthe UI thread. That throws an exception. When you interact with UI elements, it has to be on the UI thread.
这将起作用,因为TimerTick
将在 UI 线程上执行。您的单独线程想法的问题在于它Form.Close
是在后台线程而不是UI 线程上调用的。那会抛出异常。当您与 UI 元素交互时,它必须在 UI 线程上。
Your background thread idea probably would work if you called Form.Invoke
to execute the Close
.
如果您调用Form.Invoke
执行Close
.
You could also create a WaitableTimer
object and set its event for the specific time. The Framework doesn't have a WaitableTimer
, but one is available. See the article Waitable Timers in .NET with C#. Code is available at http://www.mischel.com/pubs/waitabletimer.zip
您还可以创建一个WaitableTimer
对象并在特定时间设置其事件。框架没有WaitableTimer
,但有一个可用。请参阅文章Waitable Timers in .NET with C#。代码可在http://www.mischel.com/pubs/waitabletimer.zip 获得
If you use the WaitableTimer
, be advised that the callback executes on a background thread. You'll have to Invoke
to synchronize with the UI thread:
如果您使用WaitableTimer
,请注意回调在后台线程上执行。您必须Invoke
与 UI 线程同步:
this.Invoke((MethodInvoker) delegate { this.Close(); });
回答by Dilshod
Is there "ServiceEnded" event? If yes close your form when your service ended.
是否有“ServiceEnded”事件?如果是,请在您的服务结束时关闭您的表格。
回答by Dimitar Dimitrov
How about something like this:
这样的事情怎么样:
public partial class Form1 : Form
{
private static Timer _timer = new Timer();
public Form1()
{
InitializeComponent();
_timer.Tick += _timer_Tick;
_timer.Interval = 5000; // 5 seconds
_timer.Start();
}
void _timer_Tick(object sender, EventArgs e)
{
// Exit the App here ....
Application.Exit();
}
}
回答by sa_ddam213
If you use the System.Threading.Timer
you can use the DueTime
to set the first time it fires as the time you want to close your application
如果您使用 ,则System.Threading.Timer
可以使用 将DueTime
第一次触发的时间设置为您要关闭应用程序的时间
new System.Threading.Timer((o) => Application.Exit(), null, (Configs.EndService - DateTime.Now), TimeSpan.FromSeconds(0));
Application.Run(new Form1());