简体   繁体   English

如何有效地使用计时器

[英]How do I efficently use timers

I have some Objects that Expire after a certain amount of time.我有一些在一定时间后过期的对象。 Right now I am using a Single timer to raise an event every 10 seconds and run thru the object collection and see if anything has expired.现在,我正在使用单个计时器每 10 秒引发一次事件,并通过 object 集合运行,看看是否有任何内容过期。

Instead of this I am considering adding a timer to each object and setting it to fire an event to expire at the desired time.取而代之的是,我正在考虑为每个 object 添加一个计时器,并将其设置为触发事件以在所需时间到期。 I think the most appropriate timer is the System.Timers.Timer Does any one have any thoughts on this?我认为最合适的计时器是System.Timers.Timer有人对此有任何想法吗?

I have a test rig so I will be able to compare what I have now and the refactored implementation but I would like to think this is a good idea before I start.我有一个测试台,所以我可以比较我现在拥有的和重构的实现,但我想在开始之前这是一个好主意。

I wouldn't create that many timers, because of the overhead it would be.我不会创建那么多计时器,因为它会产生开销。

Perhaps it's better to have one timer.也许最好有一个计时器。 Now this timer shouldn't trigger every 10 seconds, it should trigger when the next element expires.现在这个计时器不应该每 10 秒触发一次,它应该在下一个元素到期时触发。 so you have some kind of "just in time trigger"所以你有某种“及时触发”

If you have 200 elements and the first will expire in 2 seconds, you could have a timer of 2 seconds regardless that the last element will expire in 2 years or so...如果您有 200 个元素并且第一个元素将在 2 秒内过期,那么您可以拥有 2 秒的计时器,而不管最后一个元素将在 2 年左右过期...

As I assume the objects can't really "self destruct" when expired, I would go with the single static Timer checking what objects have expired, and Dispose them before removing from the collection.因为我假设对象在过期时不能真正“自毁”,所以我将 go 与单个 static 计时器检查哪些对象已过期,并在从集合中删除之前处理它们。

The System.Timers.Timer is good for this task, just make sure to wrap everything with try..catch because single uncaught error will cause the timer to stop ticking and you won't even get any notification for this. System.Timers.Timer非常适合这项任务,只需确保使用try..catch包装所有内容,因为单个未捕获的错误将导致计时器停止计时,您甚至不会收到任何通知。

I do prefer System.Threading.Timer over the one you mentioned.我确实更喜欢System.Threading.Timer而不是您提到的那个。 The reason is that System.Timers.Timer will consume all unhandled exceptions and therefore hide errors in your application.原因是System.Timers.Timer将消耗所有未处理的异常,因此隐藏应用程序中的错误。

I would also make a list of objects and traverse it in the timer method.我还会制作一个对象列表并在计时器方法中遍历它。 It's a sound approach and not very hard to implement (Keep It Simple and Stupid)这是一种合理的方法,实施起来并不难(保持简单和愚蠢)

The only reason to not do so is if it's important that the objects are check after exactly 10 seconds (and not 11 or 12 seconds).不这样做的唯一原因是在恰好 10 秒(而不是 11 或 12 秒)后检查对象是否很重要。 It all depends on how long each execution takes.这完全取决于每次执行需要多长时间。

Do you have a list of objects with set expiry times?你有设置过期时间的对象列表吗?

eg obj1, expires at 12:35:01 obj2, expires at 12:37:14 obj3, expires at 12:38:56 obj4, expires at 12:43:44例如 obj1,在 12:35:01 到期 obj2,在 12:37:14 到期 obj3,在 12:38:56 到期 obj4,在 12:43:44 到期

If we know the expiry times when the collection is created we can use a single timer and a list of intervals to trigger the timer when the objects need to be removed如果我们知道创建集合时的到期时间,我们可以使用单个计时器和间隔列表在需要删除对象时触发计时器

eg例如

             Wait time in seconds       To Be Removed
int1       12:35:01 - now()                obj1
int2       12:37:14 - 12:35:01             obj2
int3       12:38:56 - 12:37:14             obj3
int4       12:43:44 - 12:38:56             obj4

A) Peek the first entry in the 'delta list' (I have heard it called this before but could not give you a citation) and set the timer interval to the wait time. A)查看“增量列表”中的第一个条目(我以前听说过它叫这个但不能给你引用)并将计时器间隔设置为等待时间。

B) When the timer tiggers, remove the first entry from the delta list and do whatever needs to be done with the object from the source collection. B) 当计时器触发时,从增量列表中删除第一个条目,并使用源集合中的 object 执行任何需要执行的操作。

Back to A回到A

When an item is added to the collection you need to add a new entry to the delta list in the correct position.将项目添加到集合时,您需要将新条目添加到正确 position 中的增量列表中。 Not overly complicated but allow for the timer popping while you are in the middle of adding.不太复杂,但允许在添加过程中弹出计时器。

Pros: Only one timer.优点:只有一个计时器。 It's all you need.这就是你所需要的。 A timer per object is going to suck up a lot of resources.每个 object 的计时器会占用大量资源。

You only need to check through your whole collection at startup and then partial scans whenever new items are added.您只需在启动时检查整个集合,然后在添加新项目时进行部分扫描。

Never miss an expiry.永远不会错过到期。 It may not be important but a fixed time check means that we are late on removing some items.这可能并不重要,但固定时间的检查意味着我们在移除某些项目时迟到了。

Cons: More complicated to implement than simply scanning the whole collection on a timer pop.缺点:实现起来比简单地在计时器弹出时扫描整个集合更复杂。

Before changing anything, is the current implementation causing any problems?在更改任何内容之前,当前的实现是否会导致任何问题?

You will get a performance bump from the timer popping only when needed and from not having to scan the list each time.仅在需要时才弹出计时器,并且不必每次都扫描列表,您将获得性能提升。 Are we talking 100 entries or 100,000?我们说的是 100 个条目还是 100,000 个条目? Would the performance bump be noticeable?性能提升会很明显吗?

You will no longer have any 'late' expiries (being removed from the list after their time out).您将不再有任何“延迟”到期(在超时后从列表中删除)。 Does this matter?这有关系吗?

hth, Alan hth,艾伦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM