I'm not sure I follow how your service works. You say that the service "acts
to poll for jobs" but don't explain anything about this mechanism, other
than the rather odd fact that it sleeps instead of using a timer. In
addition, you use the plural word "jobs," but I only see one child thread
created one time. Clearly, there is more to this app than you've explained,
and the devil is in the details.
However, you do mention one thing that I can put my finger on:
Calling the method: "Stop" below causes the application to hang at the
line:
if (!m_colJobs.Running)
But I don't have any information about this property or field of the class
being referenced. How does the m_colJobs class know whether it's running or
not? Is this a property? If so, how does the getter method work?
I write a similar service, but for this service I wrote a class that handles
its own threads. The service simply calls the Start and Stop methods of the
class. The Start method tells the class to start its "Run" thread. The Stop
method tells it to stop its "Run" thread. The class notifies the service of
its state by raising various events from the child thread. The service
subscribes to the events and uses the reported state of the class, and its
various properties (set by the child thread) to determine when to exit on
stop.
But the salient point of this is that the "Run" child thread does *not*
continue indefinitely. And this is probably the crux of your problem. The
"Run" thread is a method, which means that it executes *without
interruption* until it is finished. The method raises events at various
points of its execution, as well as setting properties in its parent class,
which can be polled by the service if necessary. But the thread executes one
time and then exits. The parent class uses a timer to determine how often to
run the method, *not* an infinite loop. The timer's Elapsed event triggers
the method to run. So, there is always an opportunity to simply stop the
timer, and once the method thread has finished, it is not restarted.
This way, the service can call the "Stop" method of the class, which simply
stops the timer. The service can then wait for the "Finished" property of
the class (set by the child thread) to become true, and can then exit
gracefully.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.