定时器比System.Threading.Timer更可靠 [英] Timer more reliable than System.Threading.Timer

查看:144
本文介绍了定时器比System.Threading.Timer更可靠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的10秒间隔System.Threading.Timer。我已经将一小块code每次写入文件计时器火灾,而大部分时间是触发导通时间,有时($ P $时,应用程序的其余部分是买psumably)而没有火30或40秒,并触发了一遍又一遍临门。

I'm currently using the System.Threading.Timer on a 10 second interval. I've adding a small piece of code to write to a file each time the timer fires, and whilst most of the time it fires on-time, sometimes (presumably when the rest of the app is buy), fails to fire for 30 or 40 seconds, and fires over and over again in quick succession.

有没有更可靠的计时器我可以​​使用.NET 3.5?

Is there a more reliable timer I can use in .NET 3.5?

定时器的设置如下

Timer someTimer = new Timer(new TimerCallback(SomeMethod), null, 0, 10000);

...和回调是:

...and the callback is:

private static void SomeMethod(object state)

然而,这是难以提供更多的code比这个,因为计时器通常触发正确。当它嵌入在海量应用(〜100,000行左右),用多张线程被解雇掉,左,右和中央,你慢慢的开始看到计时器射击intermitently。 我见过几个职位提示线程池可能被耗尽,所以我目前在寻找,看看这可能是我所经历。

However, it's difficult to provide much more code than this, as the Timer usually fires correctly. When it's embedded in a massive application (~100,000 lines or so), with mulitple threads being fired off, left, right, and centre, you slowly start to see the timer firing intermitently. I've seen several posts suggesting the ThreadPool may be exhausted, so I'm currently looking in to see if this might be what I'm experiencing.

推荐答案

这听起来完全像线程池饥饿。查找出长时间运行/阻塞线程池中运行的作业,要么重写使用异步IO,或在长时间运行的CPU密集型作业的情况下,根本就没有线程池中运行这些作业。运行他们自己的线程或使用后台工作。

This sounds exactly like thread pool starvation. Look out for long running/blocking jobs running in the thread pool and either rewrite using async io, or in the case of long running CPU intensive jobs, simply don't run these jobs in the thread pool. Run them on their own thread or use a background worker.

这篇关于定时器比System.Threading.Timer更可靠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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