定期执行特定功能的应用程序 [英] Application that performs specific function at regular intervals
问题描述
我有一个编写控制台应用程序/程序/类的任务,其中应用程序必须定期运行特定的方法。
我的问题是
==>我不知道如何运行只使用有限资源在后端运行的应用程序/程序?
我在c#中尝试了Timer类,但是程序在某个时间点停止。
I have a task to write a console application/Program/Class, In which the application has to run specific method at regular intervals.
My Question is
==>I don't know how to run a application/Program that runs all the time in the back end utilizing only limited resources?
I have tried "Timer" class in c# but the program stops at some pt of time.
推荐答案
简单的方法是创建一个控制台应用程序并创建exe文件。
并使用Windows调度程序以特定间隔运行该exe文件。
Simple way is to create a console application and create the exe file.
And use the Windows scheduler to run that exe file on certain interval.
private System.Timers.Timer aTimer;
private void StartTimer(double intervalInMilleseconds)
{
aTimer = new System.Timers.Timer(intervalInMilleseconds);
aTimer.Elapsed += (sender, args) => someMethod();
aTimer.Enabled = true;
}
private void someMethod()
{
aTimer.Enabled = false;
Console.WriteLine("method called");
aTimer.Enabled = true;
}
在某处,调用:StartTimer(#intervalInMilleseconds);
除了演示目的之外,我认为这里建议使用WindowsScheduler是最好的策略。
而且,你有没有想过如何终止你的应用程序:你最好:)
Somewhere, call: StartTimer(#intervalInMilleseconds);
For anything other than "demo" purposes, I think the suggestion here to use WindowsScheduler is the best strategy.
And, have you thought about how you will terminate your application: you better :)
这篇关于定期执行特定功能的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!