在特定时间后以编程方式关闭WinForms应用程序的正确方法是什么? [英] What is the proper way to programmatically close a WinForms application after a certain time?

查看:46
本文介绍了在特定时间后以编程方式关闭WinForms应用程序的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以通常的方式启动表单:

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)在Main方法(是Application.Run()语句)中,输入以下AFTER Application.Run()

(1) In the Main method (were the Application.Run() statement is), I enter the following AFTER Application.Run()

while (DateTime.Now < Configs.EndService) { }

结果:它永远不会被击中.

RESULT: It never gets hit.

(2)在Application.Run()之前,我启动了一个新的后台线程:

(2) BEFORE Application.Run() I start a new Background Thread:

        var thread = new Thread(() => EndServiceThread()) { IsBackground = true };
        thread.Start();

其中EndServiceThread为:

where EndServiceThread is:

    public static void EndServiceThread()
    {
        while (DateTime.Now < Configs.EndService) { }
        Environment.Exit(0);
    }

结果:vshost32.exe已停止工作崩溃.

RESULT: vshost32.exe has stopped working crash.

(3)在MainForm滴答事件中:

(3) In MainForm Tick Event:

        if (DateTime.Now > Configs.EndService)
        {
            this.Close();
            //Environment.Exit(0);
        }

结果:vshost32.exe已停止工作崩溃.

RESULT: vshost32.exe has stopped working crash.

实现我的目标的正确方法是什么?同样,我要启动表格,将其打开并打开.运行到特定时间(Configs.EndService),然后关闭.

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.

谢谢你,本.

推荐答案

创建计时器,并使其在其事件处理程序中关闭程序.

Create a Timer, and have it close the program in its event handler.

说您希望应用程序在10分钟后关闭.您以60,000毫秒的周期初始化计时器.您的事件处理程序将变为:

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:

void TimerTick(object sender)
{
    this.Close();
}

如果您希望它在特定的日期和时间关闭,您可以让计时器每秒打一次,然后对照所需的结束时间检查 DateTime.Now .

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.

这将起作用,因为 TimerTick 将在UI线程上执行.您的单独线程概念的问题在于,在后台线程(UI线程不是 )上调用了 Form.Close .这引发了异常.与UI元素进行交互时,它必须位于UI线程上.

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, not the UI thread. That throws an exception. When you interact with UI elements, it has to be on the UI thread.

如果您调用 Form.Invoke 来执行 Close ,则您的后台线程想法可能会起作用.

Your background thread idea probably would work if you called Form.Invoke to execute the Close.

您还可以创建一个 WaitableTimer 对象,并将其事件设置为特定时间.该框架没有 WaitableTimer ,但是有一个可用.请参阅文章使用C#的.NET中的可用计时器.可以在 http://www.mischel.com/pubs/waitabletimer.zip 中找到代码.

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 ,请注意回调是在后台线程上执行的.您必须 Invoke 才能与UI线程同步:

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:

this.Invoke((MethodInvoker) delegate { this.Close(); });

这篇关于在特定时间后以编程方式关闭WinForms应用程序的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆