我可以创建多少个 System.Timers.Timer 实例?我可以扩展多远? [英] How many System.Timers.Timer instances can I create? How far can I scale?

查看:29
本文介绍了我可以创建多少个 System.Timers.Timer 实例?我可以扩展多远?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 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.TimerSystem.Threading.Timer 的包装器,而后者又是 Windows 的 定时器队列.定时器队列定时器非常轻量级——使用很少的系统资源.我没有扩展到数千个计时器,但数百个计时器工作得很好.根据我对实现的了解,我无法想象数千个计时器会出现任何问题.

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.

您似乎可以轻松构建一个创建 10,000 个计时器的测试程序.将每个设置为在最后一个后一秒打勾,并将其周期设置为 10,000 秒.然后坐下来看着它滴答作响.或者让您的程序跟踪谁应该下一个勾,下一个实际勾上谁.

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.

这篇关于我可以创建多少个 System.Timers.Timer 实例?我可以扩展多远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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