在一天中的特定时间触发一个事件的好办法吗? [英] Good way of firing an event at a particular time of day?

查看:441
本文介绍了在一天中的特定时间触发一个事件的好办法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要在白天断火在某些时候一对夫妇事件的应用程序 - 的时间由用户定义的所有。我能想到的一对夫妇做的方式,但没有人坐得很好。该时间并不一定是一个特别高的分辨率 - 一分钟左右,每程细

I have an app that needs to fire off a couple of events at certain times during the day - the times are all defined by the users. I can think of a couple of ways of doing it but none of them sit too well. The timing doesn't have to be of a particularly high resolution - a minute or so each way is fine.

我的思路:


  1. 在应用程序启动时读取所有的时间和启动计时器关闭,将在适当的时候

  1. When the app starts up read all the times and start timers off that will Tick at the appropriate time

启动计时器关闭该会检查每分钟左右的时事

Start a timer off that'll check every minute or so for 'current events'

为TIA没有更好的办法。

tia for any better solutions.

推荐答案


  • 商店/指数时,他们接下来需要注意排序的事件。这可能是在内存或不按照有多少,多久你做出改变,等等。如果你的所有事件的触发,每天一次,这份名单基本上是一个循环缓冲区,只有当用户更改其事件的变化。

  • 在在列表的头部事件的时间启动一个计时器,将'滴答'。四舍五入到下一分钟如果你喜欢

  • 当计时器火灾的过程,现在在过去的所有事件。,再将它们插入到列表中,如果必要的(即如果你没有循环缓冲区优化),并设置一个新的计时器。

显然,当你改变了事件集,或更改时间,现有的事件,那么你可能需要重新设置计时器,使其更早开火。有平时没有点重置它以后消防 - 你可能也只是让他走了,什么也不做。如果您在计时器上​​多久可以运行放一分钟的上限(或只是有一个1分钟重复定时器),那么你可以在1分钟的精度得到而没有重新设置。这基本上是你的选择2。

Obviously, when you change the set of events, or change the time for an existing event, then you may need to reset the timer to make it fire earlier. There's usually no point resetting it to fire later - you may as well just let it go off and do nothing. And if you put an upper limit of one minute on how long the timer can run (or just have a 1 minute recurring timer), then you can get within 1-minute accuracy without ever resetting. This is basically your option 2.

按理说你应该使用现有的框架,而不是滚动您自己,但我不知道C#,所以我不知道什么是可用的。我一般是一个有点谨慎设置定时器squillions的想法,因为有些环境不支持(或不支持得很好)。因此该方案中,其中仅需要一个。我不知道C#是否有这方面的任何问题,但这个方案可以很容易地安排使用O(1)如果RAM必要的,这不能被打败。

Arguably you should use an existing framework rather than rolling your own, but I don't know C# so I have no idea what's available. I'm generally a bit wary of the idea of setting squillions of timers, because some environments don't support that (or don't support it well). Hence this scheme, which requires only one. I don't know whether C# has any problems in that respect, but this scheme can easily be arranged to use O(1) RAM if necessary, which can't be beat.

这篇关于在一天中的特定时间触发一个事件的好办法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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