使用计时器轮询数据库 [英] Polling Database with Timer

查看:164
本文介绍了使用计时器轮询数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#编写一个需要连续不断地实时访问MySQL数据库的应用程序.我目前正在使用计时器每毫秒轮询数据库.

我假设(可能是正确的)这不是解决此问题的最有效方法...有人可以告诉我解决此问题的正确"方法,或者至少是一种更有效的方法吗?

预先感谢,
-Dee

I''m writing an application in C# that needs to access a MySQL db in real-time on a continuous basis. I''m currently polling the database every millisecond using a Timer.

I''m assuming (probably correctly) that this isn''t the most efficient way to go about this...can someone please tell me the "correct" way to go about this or least a more efficient way?

Thanks in advance,
-Dee

推荐答案

如果您正在使用计时器,则存在一个问题:轮询时间有时可能会比计时器时间长;那么当另一个计时器滴答调用您的回调或事件处理程序时,如果轮询未完成,您将怎么办?

创建一个始终循环轮询数据库的线程.现在,您可以做两件事:1)使用System.Threading.Thread.Sleep在循环中添加一些延迟; 2)使用EventWaitHandle执行节气门线程循环,并从计时器事件句柄中调用EventWaitHandle.Set.两种方式都不会浪费线程在等待或休眠状态下的CPU时间:OS会关闭线程,并且永远不要将其调度回执行,直到在到期时间(或Thread.Abort)将其唤醒之前.

我喜欢第二种,稍微复杂一点的方法.它提供了更好的时间安排.当轮询时间小于计时器周期时,轮询将与计时器周期一起发生,并且当此时间段不足以进行轮询时,该过程将变得更慢,就像继续轮询一样(无论如何都不能更快),并且轮询时间取决于查询,当前数据和其他因素,包括随机因素);在这种情况下,所有多余的计时器事件都将被忽略.

为此,可以使用类System.Threading.AutoResetEvent.您的轮询线程在此类的实例上调用方法WaitOne,计时器在同一实例上调用Set.

参见:
http://msdn.microsoft.com/en-us/library/system.threading. eventwaithandle.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading. autoresetevent.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading. thread.aspx [^ ].



看完后续问题后,另一个建议是:应将线程,计时器和事件等待句柄封装在线程包装器中,这是由于我在过去的解决方案中描述的几个原因:

如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

-SA
If you''re using the timer, there is one problem: a polling time might sometimes be longer then the timer period; so what you are going to do if the polling is incomplete when another timer tick calls your callback or event handler?

Create a thread polling the database in cycle all the time. Now, you can do two things: 1) add some delay in cycle using System.Threading.Thread.Sleep; 2) Throttle thread cycle execution with EventWaitHandle and call EventWaitHandle.Set from the timer event handle. Both ways do not waste CPU time when a thread is in a wait or sleep state: OS switched the thread off and never schedule it back to execution until it is waken up by the expiration time (or Thread.Abort).

I prefer the second, a little more sophisticated way. It provides better timing. When a polling time is less then the timer period, the polling happens with the period of the timer, and when this period of time is not enough for polling, the process becomes just slower as in continues polling (it cannot be faster anyway, and the polling time depends on a query, current data and other factors, including random factors); in this case all redundant timer events are just ignored.

For this purpose, you can use the class System.Threading.AutoResetEvent. Your polling thread calls the method WaitOne on the instance of this class, and the timer calls Set of the same instance.

See:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[^].



After looking at the follow-up question, another suggestion: the thread, the timer and the event wait handle should be encapsulated in the thread wrapper, by several reasons I describe in my past solutions:

How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

—SA


如果您知道要查找的内容,可以使用:http://www.devart.com/dotconnect/mysql/docs/Devart. Data.MySql〜Devart.Data.MySql.MySqlDependency.html

这可以为您进行轮询,并且可能(或可能不会;))使轮询效率更高.

干杯阿迪.
If you know what you are looking for you can use: http://www.devart.com/dotconnect/mysql/docs/Devart.Data.MySql~Devart.Data.MySql.MySqlDependency.html

This can do the polling for you and may ( or may not ;) )do it more efficient.

Cheers Addy.


这篇关于使用计时器轮询数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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