为什么Timer的Elapsed事件永远不会被解雇?

时间:2016-05-24 16:27:37

标签: c# timer windows-services elapsed

我在Windows服务中获得了此代码:

public void ConfigureService()
{
    //timer = new Timer { Interval = 600000 };
    timer = new Timer { Interval = 10000 };
    // 10 minutes; 1 second while testing
    timer.Elapsed += timer_Tick;
    timer.Enabled = true;
    RoboRprtrLib.WriteToLog("RoboRprtrService has started");
}

private void timer_Tick(object sender, ElapsedEventArgs eeargs)
{
    timer.Elapsed -= timer_Tick;
    try
    {
        RoboRprtrLib.WriteToLog("Timer has ticked");
        RoboRprtrLib.GenerateAndEmailDueReports();
    }
    finally
    {
        timer.Elapsed += timer_Tick;
    }
}

从Program.cs调用ConfigureService():

static void Main()
{
    // got this idea ("How to Debug or Test your Windows Service Without Installing it...") from http://www.codeproject.com/Tips/261190/How-to-Debug-or-Test-your-Windows-Service-Without
    #if(!DEBUG)
    var ServicesToRun = new ServiceBase[] 
    { 
        new RoboRprtrService() 
    };
    ServiceBase.Run(ServicesToRun);
    #else
    var rrs = new RoboRprtrService();
    rrs.ConfigureService();
    #endif
}

我在这一行的ConfigureService()中有一个断点:

timer = new Timer { Interval = 10000 };

达到了;我可以逐步完成整个ConfigureService()方法。

我在第一行的Elapsed / Tick事件中有一个断点:

timer.Elapsed -= timer_Tick;

......永远不会到达。

为什么不呢?计时器是否设置为在10秒后跳闸,此时应该调用Tick事件处理程序?

更新

这是从ServiceBase派生的类的完整代码:

public partial class RoboRprtrService : ServiceBase
{
    private Timer timer;

    public RoboRprtrService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        ConfigureService();
    }

    public void ConfigureService()
    {
        //timer = new Timer { Interval = 600000 };
        timer = new Timer { Interval = 50000 };
        // 10 minutes; 50 seconds while testing
        timer.Elapsed += timer_Tick;
        timer.Enabled = true;
        RoboRprtrLib.WriteToLog("RoboRprtrService has started");
    }

    private void timer_Tick(object sender, ElapsedEventArgs eeargs)
    {
        timer.Elapsed -= timer_Tick;
        try
        {
            RoboRprtrLib.WriteToLog("Timer has ticked");
            RoboRprtrLib.GenerateAndEmailDueReports();
        }
        finally
        {
            timer.Elapsed += timer_Tick;
        }
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
        RoboRprtrLib.WriteToLog("RoboRprtrService has stopped");
    }

}

更新2

这对我来说似乎很奇怪,但如果我添加这一行:

RoboRprtrLib.GenerateAndEmailDueReports();

...在ConfigureService()方法结束时,计时器 最终会被触发。

更新3

更奇怪的是:我收到了关于需要添加STAThread属性的错误消息(并且正在调用一个不应该被调用的方法,这导致它失败并且服务崩溃)。所以我在Program.cs中使用“[STAThread]”修饰了Main(),现在它可以正常工作。

在操作过程中,计时器跳闸了几次,但是如果正在进行处理,我会退出代码。当被调用的方法完成时,IDE“闪过”,好像在说“噗!我离开这里!”

所以我的Program.cs代码现在是:

[STAThread]
static void Main()
{
    #if(!DEBUG)
    var ServicesToRun = new ServiceBase[] 
    { 
        new RoboRprtrService() 
    };
    ServiceBase.Run(ServicesToRun);
    #else
    var rrs = new RoboRprtrService();
    rrs.ConfigureService();
    Console.ReadLine();
    #endif
}

...并且从ServiceBase派生的类中最相关的代码是:

public void ConfigureService()
{
    //timer = new Timer { Interval = 600000 };
    timer = new Timer { Interval = 50000 };
    // 10 minutes; 50 seconds while testing
    timer.Elapsed += timer_Tick;
    timer.Enabled = true;
    RoboRprtrLib.WriteToLog("RoboRprtrService has started");
    operationIsRunning = true;
    RoboRprtrLib.GenerateAndEmailDueReports();
    operationIsRunning = false;
}

private void timer_Tick(object sender, ElapsedEventArgs eeargs)
{
    if (operationIsRunning) return;
    operationIsRunning = true;
    . . .

“if(operationIsRunning)return;”我执行GenerateAndEmailDueReports()时,在上一次运行期间,tick处理程序中的断点已达到三次,并且每次都按设计返回。

1 个答案:

答案 0 :(得分:2)

正如Hans Passant在评论中所述,在调试模式下,您的代码将在调用Firemonkey后立即终止,因此ConfigureService没有时间执行。

在发布模式下timer阻止线程直到服务结束,但这在调试版本中不会发生。

修改

实际上我尝试使用ServiceBase.Run并且没有停止,显然标准输入被重定向,只是尝试保持进程运行,无限循环或其他什么。

像这样:

Console.ReadLine()