在Windows服务中使用的最佳计时器

108

我需要创建一些Windows服务,每过N段时间就会执行。
问题是:
我应该使用哪个计时器控件:System.Timers.Timer还是System.Threading.Timer?这会对什么产生影响吗?

我之所以问是因为我听到了很多关于System.Timers.Timer在Windows服务中无法正确工作的证据。
谢谢。

6个回答

118

无论是System.Timers.Timer还是System.Threading.Timer都适用于服务。

你应该避免使用的计时器是System.Web.UI.TimerSystem.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
...
 */
如果您选择使用 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();
        }
    }
}

这两个例子都来自于MSDN页面。


1
为什么您建议使用GC.KeepAlive(aTimer);,aTimer是实例变量,如果它是窗体的实例变量,那么只要窗体存在,就会始终引用它,不是吗? - Giorgi Moniava

37
不要使用服务来完成此操作。创建一个普通应用程序并创建一个定期运行它的计划任务。
这是常见的最佳实践。Jon Galloway同意我的看法。或者也可能是相反的。无论如何,事实是,创建一个Windows服务来执行定时器间歇性任务不是最佳实践。

"如果你正在编写一个运行计时器的Windows服务,你应该重新评估你的解决方案。"

–Jon Galloway,ASP.NET MVC社区项目经理,作者,兼职超级英雄


26
如果您的服务需要全天候运行,那么使用服务可能比使用定时任务更加合适。或者,对于基础设施团队不如应用程序团队那么熟练的人员来说,使用服务可能会简化管理和日志记录。然而,对于是否需要使用服务的假设进行质疑是完全合理的,这些负面评价是不应该的。对两者都点赞(+1)。 - sfuqua
4
@M.R.:我不是个传教士,而是一个现实主义者。实际情况告诉我,计划任务并非“极其不稳定”。如果你声称计划任务不可靠,那么你就需要提供支持证据。否则,你只是在散播恐惧、不确定性和怀疑。 - user1228
13
我不太想卷入这个泥潭,但我必须稍微为M.R.辩护一下。我在公司运行着几个关键应用程序,它们都是Windows控制台应用程序。我使用Windows任务计划程序来运行它们。至少有5次,我们遇到了调度程序服务“某种方式混乱”的问题。任务没有执行,有些处于奇怪的状态。唯一的解决方案是重新启动服务器或停止和启动调度程序服务。这是一个生产环境中不能接受且我没有管理员权限去做的事情。仅仅是我自己的看法。 - SpaceCowboy74
6
这种情况其实在我加入的几个服务器上(到目前为止有三个)都出现过。虽然不能说这是常态,但有时候自己实现某个方法也不错。 - SpaceCowboy74
2
@Nick:这要看情况。如果你需要一个网站,让人们可以更改日程安排,那么就需要一个服务。或者,如果它只需要在每次安装时配置一次,或者只需要很少地运行,那么一个定时任务就足够了。 - user1228
显示剩余8条评论

7

任何一种都可以正常工作。实际上,System.Threading.Timer在内部使用了System.Timers.Timer。

话虽如此,不正确地使用System.Timers.Timer很容易。如果您没有在某个变量中存储Timer对象,那么它可能会被垃圾回收。如果发生这种情况,您的计时器将不再触发。调用Dispose方法停止计时器,或使用System.Threading.Timer类,它是一个稍微更好的封装。

到目前为止,您遇到了哪些问题?


让我想知道为什么Windows Phone应用程序只能访问System.Threading.Timer。 - Stonetip
Windows手机可能有一个更轻量级的框架版本,因为不需要使用两种方法来使用所有额外的代码,所以它们不会被包含在内。我认为Nick的答案更好地解释了为什么Windows手机无法访问System.Timers.Timer,因为它无法处理抛出的异常。 - Malachi

2

我同意前面的评论,最好考虑一种不同的方法。我的建议是编写一个控制台应用程序并使用Windows计划任务程序:

这将会:

  • 减少复制计划任务行为的代码
  • 在调度行为方面提供更大的灵活性(例如仅在周末运行),所有调度逻辑都从应用程序代码中抽象出来
  • 利用命令行参数作为参数,而无需在配置文件等中设置配置值
  • 在开发过程中更容易进行调试/测试
  • 允许支持用户通过直接调用控制台应用程序来执行(例如在支持情况下非常有用)

3
但是这需要已登录的用户?因此,如果需要在服务器上全天候运行,服务可能更好。 - JP Hellemons

1
如前所述,System.Threading.TimerSystem.Timers.Timer都可以工作。两者之间的主要区别在于System.Threading.Timer是对另一个计时器的包装。

System.Threading.Timer会有更多的异常处理,而System.Timers.Timer将吞噬所有异常。

这在过去给我带来了很大的问题,因此我总是使用"System.Threading.Timer"并且仍然非常好地处理您的异常。

0

我知道这个帖子有点老了,但它对我有用,我认为值得注意的是还有另一个原因为什么System.Threading.Timer可能是一个好的方法。 当您需要定期执行可能需要很长时间的作业,并且希望确保在作业之间使用整个等待期间,或者如果您不希望在作业花费的时间超过计时器周期的情况下再次运行作业。 您可以使用以下内容:

using System;
using System.ServiceProcess;
using System.Threading;

    public partial class TimerExampleService : ServiceBase
    {
        private AutoResetEvent AutoEventInstance { get; set; }
        private StatusChecker StatusCheckerInstance { get; set; }
        private Timer StateTimer { get; set; }
        public int TimerInterval { get; set; }

        public CaseIndexingService()
        {
            InitializeComponent();
            TimerInterval = 300000;
        }

        protected override void OnStart(string[] args)
        {
            AutoEventInstance = new AutoResetEvent(false);
            StatusCheckerInstance = new StatusChecker();

            // Create the delegate that invokes methods for the timer.
            TimerCallback timerDelegate =
                new TimerCallback(StatusCheckerInstance.CheckStatus);

            // Create a timer that signals the delegate to invoke 
            // 1.CheckStatus immediately, 
            // 2.Wait until the job is finished,
            // 3.then wait 5 minutes before executing again. 
            // 4.Repeat from point 2.
            Console.WriteLine("{0} Creating timer.\n",
                DateTime.Now.ToString("h:mm:ss.fff"));
            //Start Immediately but don't run again.
            StateTimer = new Timer(timerDelegate, AutoEventInstance, 0, Timeout.Infinite);
            while (StateTimer != null)
            {
                //Wait until the job is done
                AutoEventInstance.WaitOne();
                //Wait for 5 minutes before starting the job again.
                StateTimer.Change(TimerInterval, Timeout.Infinite);
            }
            //If the Job somehow takes longer than 5 minutes to complete then it wont matter because we will always wait another 5 minutes before running again.
        }

        protected override void OnStop()
        {
            StateTimer.Dispose();
        }
    }

    class StatusChecker
        {

            public StatusChecker()
            {
            }

            // This method is called by the timer delegate.
            public void CheckStatus(Object stateInfo)
            {
                AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
                Console.WriteLine("{0} Start Checking status.",
                    DateTime.Now.ToString("h:mm:ss.fff"));
                //This job takes time to run. For example purposes, I put a delay in here.
                int milliseconds = 5000;
                Thread.Sleep(milliseconds);
                //Job is now done running and the timer can now be reset to wait for the next interval
                Console.WriteLine("{0} Done Checking status.",
                    DateTime.Now.ToString("h:mm:ss.fff"));
                autoEvent.Set();
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接