Re: threading question
"scott mcfadden" <nospam.smcfadden@criticaltech.com> wrote in message
news:eYxwCwW6GHA.3476@TK2MSFTNGP02.phx.gbl...
My question arose while attempting to porting some C# service framework
type code to Native C++. Specifically, when the service is started the
main threads kicks off a worker thread (void ServiceThread()). The main
thread then blocks indefinately until the ServiceThread terminates. The
ServiceThread may occassionally put himself (herself) to sleep if there is
no work to do or it's time to sleep so that nightly backups can take
place. The sticky part is that an admin may try to stop the Service /
ServiceThread during a sleep cycle. A thread sleep cycle could be several
minutes. Don't want windows SCM stop cmd to timeout waiting for
ServiceThread to wake up and see that it is quitting time (based upon some
boolean member variable). In managed code, during the stop event, I would
signal a stop by setting some boolean member variable to false. I would
then check the status of the worker thread to see if he/she was in
WaitSleepJoin state. If he/she was in WaitSleepJoin I would then do a
"Thread.Interupt" and then do a Thread.Join from the Stop thread to the
worker thread in order to block the Stop event from completing until the
worker thread could gracefully exit. This would cause a
ThreadInteruptedException on the worker thread which I would catch and
then gracefully exit the workerthread. This works very well in managed
code just not sure how to model it in native code.
I should tell you that while I have written my share of native services over
the years I have _never_ written one in C#. It may be bias on my part, but
rather than trying to port a service framework in C# to C++. I'd start with
a C++ sample to create a framework.
As I said, that might not be the best way though if it is the case that
there is nothing between a C# service and the native Service Control Manager
it just might be.
There is the shell of a simple service in the SDK's
Samples\winbase\Service
folder. It's an old _C_ sample that has been there forever.
You find a comparison of C# and C++ services here:
http://weblogs.asp.net/kennykerr/archive/2004/05/18/134342.aspx
and in the post the author sketches a C++ class that resembles the C# way of
doing things.
Finally, as all services have to register with the service control manager
(SCM), you can search for service related links using the search term
StartServiceCtrlDispatcher
thanks!
You are welcome.
Regards,
Will