On Mar 6, 11:14 pm, "Joseph Geretz" <jger...@nospam.comwrote:
Quote:
I have a Service which runs OK, but I'm abviously not starting it properly.
In my OnStart event I commence a long running process which polls a database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct feedback
that the service has started properly. Consequently, the SCM throws an error
dialog and the service is marked as 'Starting', even though the service is
in fact running properly. I'd like to correct this.
>
I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to do
this.
>
Also, any advice on how the service can execute at a low background priority
will be very much appreciated.
>
Thanks for your advice!
>
- Joseph Geretz -
Just create a thread in the onstart routine, and perform your work in
the thread method... You can also set that threads priority.
Something like:
// do resource aquisition
// create your thread, and set the priority, and start it up
Thread worker = new Thread (new ThreadStart (WorkerMethod));
worker.Priority = ThreadPriority.BelowNormal; // Or Lowest if you like
worker.Start();
Then you just do your actual work in the WorkerMethod. You can signal
the thread to end from the OnStop method, etc.
HTH
--
Tom Shelton