Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Polling Cycles

I'm setting up a system whereby a service is polling a database looking for information. What I'm attempting to do is set up a polling cycle so that the webservice won't be hitting the database every 30 seconds. For this cycle, after the service has started up I want it to poll the database at the 1 minute mark, if nothing is found, then it should poll the database 2 minutes later. This doubling of the time between polls should continue until the 1 hour mark is reached at which time it would reset to 1 minute.

What would be the best way to accomplish this task?

Laurence -
[614 byte] By [Hunterlmc] at [2007-11-11 7:36:36]
# 1 Re: Polling Cycles
The following should get you started:

http://www.codeguru.com/vb/gen/vb_system/services/article.php/c4833/
pclement at 2007-11-11 21:48:55 >
# 2 Re: Polling Cycles
Thanks for the site. I noticed that he was using the timer control. Would it be possible to use the "thread.sleep()" command also?

L-
Hunterlmc at 2007-11-11 21:49:50 >
# 3 Re: Polling Cycles
Is there a reason why you need to use Thread.Sleep?
pclement at 2007-11-11 21:50:49 >
# 4 Re: Polling Cycles
I don't need to use Thread.Sleep(). I could use the timer method described in the web site. However, I try not to use controls when I don't need to. It's my own hangup.
Hunterlmc at 2007-11-11 21:51:57 >
# 5 Re: Polling Cycles
You don't have to use a Timer control, just use the Timer class. The following should help:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingtimerclasstopic.asp

With respect to Thread.Sleep, in order to use it you would probably have to spawn a worker thread to handle your polling code. If you don't use a worker thread then your service will not likely respond to stop and start events. I haven't checked the below code, but it may contain a solution to what I'm referring to:

http://blogs.snapsis.com/PermaLink,guid,5b99a8aa-3516-4a45-84d2-4707b08cba86.aspx
pclement at 2007-11-11 21:52:56 >