带有计时器的 .NET Windows 服务停止响应 [英] .NET Windows Service with timer stops responding

查看:21
本文介绍了带有计时器的 .NET Windows 服务停止响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 c# 编写的 Windows 服务.它内部有一个计时器,可以定期触发一些功能.所以我的服务的骨架:

I have a windows service written in c#. It has a timer inside, which fires some functions on a regular basis. So the skeleton of my service:

public partial class ArchiveService : ServiceBase
{
    Timer tickTack;
    int interval = 10;
    ...
 
    protected override void OnStart(string[] args)
    {
        tickTack = new Timer(1000 * interval);

        tickTack.Elapsed += new ElapsedEventHandler(tickTack_Elapsed);
        tickTack.Start();
    }

    protected override void OnStop()
    {            
        tickTack.Stop();
    }    
    
    private void tickTack_Elapsed(object sender, ElapsedEventArgs e)
    {
        ...
    }
}

它会工作一段时间(例如 10-15 天)然后停止.我的意思是该服务显示为正在运行,但它没有做任何事情.我做了一些日志记录,问题可能出在计时器上,因为在间隔之后它不会调用 tickTack_Elapsed 函数.

It works for some time (like 10-15 days) then it stops. I mean the service shows as running, but it does not do anything. I make some logging and the problem can be the timer, because after the interval it does not call the tickTack_Elapsed function.

我正在考虑在没有计时器的情况下重写它,使用无限循环,在我设置的时间内停止处理.这也不是一个优雅的解决方案,我认为它可能会对内存产生一些副作用.

I was thinking about rewrite it without a timer, using an endless loop, which stops the processing for the amount of time I set up. This is also not an elegant solution and I think it can have some side effects regarding memory.

Timer 是从 System.Timers 命名空间使用的,环境是 Windows 2003.我在不同服务器上的两个不同服务中使用了这种方法,但两者都产生了这种行为(这就是为什么我认为它以某种方式连接到我的代码或框架本身).

The Timer is used from the System.Timers namespace, the environment is Windows 2003. I used this approach in two different services on different servers, but both is producing this behavior (this is why I thought that it is somehow connected to my code or the framework itself).

有人遇到过这种行为吗?有什么问题?

Does somebody experienced this behavior? What can be wrong?

我编辑了这两项服务.一个人在任何地方都得到了很好的尝试捕获和更多的日志记录.第二个定期进行计时器娱乐.自从他们之后,他们都没有停止过,所以如果这种情况再持续一周,我将结束这个问题.到目前为止,谢谢大家.

I edited both services. One got a nice try-catch everywhere and more logging. The second got a timer-recreation on a regular basis. None of them stopped since them, so if this situation remains for another week, I will close this question. Thank you for everyone so far.

我关闭这个问题,因为什么都没发生.我的意思是我做了一些更改,但这些更改与此事无关,并且这两个服务从那时起都没有出现任何问题.请将其标记为因不再相关而关闭".

I close this question because nothing happened. I mean I made some changes, but those changes are not really relevant in this matter and both services are running without any problem since then. Please mark it as "Closed for not relevant anymore".

推荐答案

就像许多受访者指出的异常被计时器吞噬一样.在我的 Windows 服务中,我使用 System.Threading.Timer.它具有 Change(...) 方法,可让您启动/停止该计时器.可能发生异常的地方可能是重入问题 - 如果 tickTack_Elapsed 执行时间长于计时器周期.通常我这样写定时器循环:

Like many respondents have pointed out exceptions are swallowed by timer. In my windows services I use System.Threading.Timer. It has Change(...) method which allows you to start/stop that timer. Possible place for exception could be reentrancy problem - in case when tickTack_Elapsed executes longer than timer period. Usually I write timer loop like this:

    void TimeLoop(object arg)
    {
        stopTimer();

        //Do some stuff

        startTimer();
    }

你也可以lock(...)你的主循环来防止重入.

You could also lock(...) your main loop to protect against reentrancy.

这篇关于带有计时器的 .NET Windows 服务停止响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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