我多少System.Timers.Timer的情况下,可以创造?我多远可以扩展?多远、情况下、System、Timers

2023-09-04 12:15:48 作者:學會疼洎記

我有一个应用程序,它利用 System.Timers.Timer的 对象做到期和电子邮件通知和这样的。目前,该系统有几百定时器活一次,但我们将要扩大应用程序的使用,这个数字可能会开始扩展到数千个(可能不超过10,000)。

I have an application which makes use of System.Timers.Timer objects to do expirations and email notifications and such. Currently the system has a couple hundred timers alive at once, but we are going to be expanding the usage of the app and that number might start scaling into the thousands (probably no higher than 10,000).

我找不到关于加强定时器的数的任何信息,所以我认为它不会成为一个问题。有谁知道这将是一个问题,我应该积极考虑改变我的方式处理过期?

I can't find any information on scaling up the number of Timers so I assume it is not going to be a problem. Does anyone know if this is going to be a problem and I should proactively look into changing the way I handle the expirations?

推荐答案

一个有点晚了这一点,我知道,但。 。

A little late on this, I know, but . . .

System.Timers.Timer的 System.Threading.Timer 的包装,这又是一个。围绕Windows定时器队列 NET包装。定时器队列定时器是非常轻量级 - 使用系统资源很少。我还没有扩展到上千定时器,但数百个定时器的工作就好了。从一点点我已经能够收集到的实施,我不能想象会有成千上万定时器的任何麻烦。

System.Timers.Timer is a wrapper around System.Threading.Timer, which in turn is a .NET wrapper around Windows' Timer Queues. Timer queue timers are very lightweight--use few system resources. I've not scaled to thousands of timers, but hundreds of timers works just fine. From what little I've been able to glean of the implementation, I can't imagine that there would be any trouble with thousands of timers.

有可能在定时器,你可以在任何特定的定时器队列的数量一定限制。我不知道限制是多少。当我在一年多前调查这些计时器,它看起来像在.NET Framework创建每个进程一个定时器队列(或者也许是每个应用程序域),以及您创建的所有定时器被放入一个队列中。

There's probably some limit on the number of timers you can have in any particular timer queue. I don't know what that limit is. When I investigated these timers a year or so ago, it looked like the .NET Framework creates one timer queue per process (or maybe it was per app domain), and all the timers you create are put into that one queue.

这可能是没有意义的通过创建自己的系统,它利用一个单一的计时器和某种类型的优先级队列机制来控制什么所谓的旁边重复定时器队列功能。这是定时器队列做什么你了。

It probably doesn't make sense to duplicate the timer queue functionality by creating your own system that uses a single timer and some type of priority queue mechanism to control what's called next. That's what the timer queue does for you already.

好像你可以轻松地构建,创建万计时器测试程序。设置每一项后,最后勾选一秒,并设置其周期10000秒。然后坐下来,看着它打勾。或者让你的程序跟踪谁应该剔下一个,谁真正蜱旁边的。

It seems like you could easily build a test program that creates 10,000 timers. Set each one to tick one second after the last, and set its period to 10,000 seconds. Then sit back and watch it tick. Or have your program keep track of who should tick next and who actually ticks next.