简体   繁体   中英

C#: use Timer in smart way

Now I understand it's two possible cases to use Timer:

  1. do some action with specified interval, it's quite easy.
  2. another usage (and more interesting for me): for instance we have array of times [11.24, 15.06, 17.47] (and it's possible to add more times there). We need to execute some operation at this moments. It looks like here I need timer with 1 minute interval to compare current hours and minutes with all items from array. And in case of TRUE execute action. But what if this operation takes 2 minutes for instance? In this case it's possible to miss some item from array. I think solution here is to separate logic for queueing (timer with 1 min interval to check time and add to queue) and logic for listening queue and execute action.

What do you think?

Option 3: Calculate how long (from now) until each item in the array - and set a timer for each interval. There is no "minute polling" so no chance of missing a time.

Option 4: Use one of the many pre-canned "Scheduler" libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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