每2分钟重新启动一次Windows应用程序 [英] Restart the windows application every 2 mins

查看:161
本文介绍了每2分钟重新启动一次Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的系统中安装了一个使用Windows窗体开发的应用程序.每隔2分钟,应用程序应退出并启动.谁能帮我提供示例代码

I have an application installed in my system which is developed using windows forms. Every 2 mins the application should exit and start. Can anyone please help me with the sample code

推荐答案

尝试以下代码:
Try this code:
private Timer timer;
private int count;
private void Form1_Load(object sender, EventArgs e)
{
    count = 0;
    timer = new Timer();
    timer.Interval = 1000;//1 second for tick
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
    count++;
    if (count == 120)//2 minutes to restart application
    {
        count = 0;
        timer.Stop();
        Application.Restart();
    }
}


为什么必须重新启动?为什么不能每两分钟重新初始化所有内容(包括可能是程序初始化的一部分的加载和数据文件)?

编辑===============

如果您未提出问题中的所有要求,则不会立即获得正确的答案.

如果您的应用程序是从中央程序中生成应用程序的,则不需要计时器-您需要做的只是响应适当的Process对象事件,该事件指示生成的进程何时终止.那时,您可以重新启动它.
Why does it have to restart? Why can''t you just reintitialize everything every two minutes (including loading and data files that might be part of the program''s initialization?

EDIT ===============

If you don''t present ALL of the requirements in your question, you won''t get a proper answer right away.

If your app is spawning the apps from a central program, you don''t need a timer - all you need to do is respond to the appropriate Process object event that indicates when a spawned process terminates. At that time, you can restart it.


最简单的方法可能是让另一个应用程序一直运行,并使其重新启动,然后每隔两(两)分钟关闭第一个应用程序.

但是,如果要运行需要间隔一小段时间后重新启动的应用程序,则可能要考虑Windows服务.
The simplesst way could be to have another application running all the time and have it restart and close the first one every few (two) minutes.

However, if you intend to run an application that needs to restart after a small interval you might want to consider windows services.


这篇关于每2分钟重新启动一次Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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