108 c# windows-services timer
我需要创建一些Windows服务,每隔N段执行一次.
问题是:
我应该使用哪个定时器控件:System.Timers.Timer或者System.Threading.Timer一个?它对某些事物有影响吗?
我问,因为我听到许多证据表明System.Timers.TimerWindows服务中的工作不正确.
谢谢.
And*_*ore 118
双方System.Timers.Timer并System.Threading.Timer会为服务工作.
您想要避免的计时器是System.Web.UI.Timer和System.Windows.Forms.Timer,分别用于ASP应用程序和WinForms.使用这些将导致服务加载一个额外的程序集,这对于您正在构建的应用程序类型并不是真正需要的.
使用System.Timers.Timer如下例子(同样,确保使用类级别变量来防止垃圾收集,如Tim Robinson的回答所述):
using System;
using System.Timers;
public class Timer1
{
private static System.Timers.Timer aTimer;
public static void Main()
{
// Normally, the timer is declared at the class level,
// so that it stays in scope as long as it is needed.
// If the timer is declared in a long-running method,
// KeepAlive must be used to prevent the JIT compiler
// from allowing aggressive garbage collection to occur
// before the method ends. (See end of method.)
//System.Timers.Timer aTimer;
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
// If the timer is declared in a long-running method, use
// KeepAlive to prevent garbage collection from occurring
// before the method ends.
//GC.KeepAlive(aTimer);
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
/* This code example produces output similar to the following:
Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
*/
Run Code Online (Sandbox Code Playgroud)
如果您选择System.Threading.Timer,您可以使用如下:
using System;
using System.Threading;
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate =
new TimerCallback(statusChecker.CheckStatus);
// Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer =
new Timer(timerDelegate, autoEvent, 1000, 250);
// When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period.\n");
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}
class StatusChecker
{
int invokeCount, maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这两个示例都来自MSDN页面.
小智 37
不要为此使用服务.创建一个普通的应用程序并创建一个计划任务来运行它.
这是最常见的最佳做法. Jon Galloway同意我的看法.或者也许是相反的方式. 无论哪种方式,事实是创建一个Windows服务来执行从计时器运行的间歇性任务不是最好的做法.
"如果您正在编写运行计时器的Windows服务,则应重新评估您的解决方案."
-Jon Galloway,ASP.NET MVC社区项目经理,作者,兼职超级英雄
任何一个都应该工作正常.实际上,System.Threading.Timer在内部使用System.Timers.Timer.
话虽如此,很容易误用System.Timers.Timer.如果你没有将Timer对象存储在某个变量中,那么它很容易被垃圾收集.如果发生这种情况,您的计时器将不再发射.调用Dispose方法来停止计时器,或使用System.Threading.Timer类,这是一个稍好的包装器.
到目前为止你看到了什么问题?
| 归档时间: |
|
| 查看次数: |
177237 次 |
| 最近记录: |