VB.NET windows service that queries a database

I am developing in VB using VS 2008.
Requirement:
Need to query a table to get a work queue id.  Need to run query every 60 seconds.  If query returns id, then do some work...if result set is empty then wait 60 seconds (or some time interval) before executing query again.  This task needs to run in the background on a workstation.

After browsing experts exchange, google, etc..., it seems that a Windows Service should be created for this task instead of a console app.    

Looking for examples to get me started in creating the windows service.  

I have the vb.net code to query the database.  So, just looking for an example of how to contruct the service.  Should this code be placed in the OnStart() or should I create another class/module/function and call from/to?  

Should I have an infinite loop that executes the query every 60 seconds in the OnStart() or is that not needed since the windows service inherently is supposed to run "forever"?  Is this how the "Timer" comes into play?  How do I add the System Timer instead of the Forms Timer?

Recommendations? Thoughts?  Examples?

Thanks in advance,
Jeff




j420exe1Asked:
Who is Participating?
 
lazyberezovskyConnect With a Mentor Commented:
Here is C# example of using System.Timer on Windows Service:
http://www.aspfree.com/c/a/C-Sharp/Timer-Objects-in-Windows-Services-with-C-sharp-dot-NET/

And less descriptive, but VB.NET
http://www.java2s.com/Tutorial/VB/0440__Windows/Servicetimer.htm

Core thoughts:
1) create System.Timer
2) set its interval to 60000
3) subscribe to it's Elapsed event (add code that runs query)
4) set timer's Enabled property to true on service start/continue and to false on pause/stop
0
 
käµfm³d 👽Commented:
The OnStart() method MUST return within about 10 - 15 seconds (otherwise Win automatically shuts it down) in order for your service to be started. To do tasks in a service, you either need to spawn a new thread or use a timer as lazyberezovsky described.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.