Click to See Complete Forum and Search --> : Windows Service


sunsilk10
September 14th, 2009, 07:59 AM
Hello,

I am creating an interface to update a database table from a different database using ODBC in VB.net. I have deployed the application as a windows service. All is working fine. However, I want to check changes in the source database continuously (i.e. as soon as the table is updated, check again) If I add a while loop in OnStart, I cannot start the service as it never ends.
Any suggestions?

Arjay
September 14th, 2009, 01:54 PM
In the OnStart method create a worker thread that does the actual db work. Also create an AutoResetEvent field called _stopEvent.

In the worker thread proc, create a WaitHandle array and pass in the _stopEvent as an array member. Then in a loop, call waitHandle.WaitOne( <timeoutvalue> ). If that method returns a zero (meaning the stopevent event has been set, exit the thread; otherwise perform the database operation.

In the OnStop method, call _StopEvent.Set( ) to signal the worker thread to exit.