I need to Stop The Service Gracefully
ive managed to stop all the threads gracefully
but im having problems gracefully, stopping the service
small example below
the code below is not the actual code but a way to see whats going on
hope it helps
//on start of a service
public partial class Myservice //inherits in designer
{
MyThreadManager ThreadManager=null;
protected void onStart()
{
if (ThreadManager==null) // 1st call on start of service
{
ThreadManager= new MyThreadManager()
if (ThreadManager.InitThreads()==true) // read
config file, create threads 1 to many, check sql connection
{
ThreadManager.EnableTimers() // starts kicking
off Jobs
}
}
}
}
/// code in theread Manager
public Class ThreadManager
{
private ThreadWorkers[] _aThreads // inherits BackGroundWorker
private Systems.Timers.Timer[] _aTimers
private MyLogger Log;
public ThreadManager()
{
}
public InitThreads()
{
//.. read config
// set up logging
this.log = new MyLogger();
this.Log.CriticalEventHandelerDelegate+=CriticalEvent
// method of this class
//check sql connection
//create threads and initilize threads
//CreateTimers
{
public void RunJobType1() // called by timer[0]
{
this._aTimer[0].Enable=false; //below kicks off
all jobs of this jobtype
for (int x=0:x<_aThreads.Length;x++}
{
if (_aThreads[x].Jobtype=1 &&
_aThreads[x].isBusy==false
&&
this._StopRunning==False)
{
int TmpId=
this.GetNewJobId()
if (TmpId>0)
{
this._aThread[x].JobId=TmpId;
this._aThread[x].RunWorkerAsync();
}
{
{
}
//so a job has been kicked off above and down somwhere a fatal
event occurs i have a Event in the logging Class that if connection error
query error occurs when writing logs
//fires a CriticalFailure Event..
public void CriticalEvent(CriticalEventargs e)
{
// all the threads come here on critical events
lock (this)
{
if (this._StopRunning==false); // lets all threads know
not to work
{
this._StopRunning=True;
this.StopWorkers() // waits for each thread to
finish its job...the jobs dont matter if thery error or not the worker
always complets , errhandling is done in the business logic
//thread just runs
the business logic class
// once im back here...i am ready to shut down the
Service....as u see im below the service class i have a Reference to the
service class ._Service
//when i try to stop the service the executable just
hangs...
whats the best way to stop a service from this
point...or do i need to get back up to the service level
{
}
}
}
Analizer1 said:
thanks alot peter
I see where u are going...and i do have a boolean flag...
for telling the threads to stop...
i will work that flag into the errorhandling
the threads should not know the difference , since
if i have 4 threads running the errorhandler will set the flag once , even
though the errhandler was called 4 times
on any error each thread stops anyway...but the manager i wrote, starts a
new job for existing , non busy threads...
so just setting this flag in the errorhandler should do the trick
thanks alot
much appriciated
Peter Duniho said:
[...]
what i want to do is stop the threads (not destroy)
and tell the service to stop
any advice on how to accomplish this.....
You need to make your algorithm in the threads interruptible. What an
appropriate way to do this depends a lot on how you've implemented the
threads. But a C# event has little to do with it one way or the other.
The easiest, most straightforward mechanism would be to create a volatile
bool variable that each thread checks every now and then to see if it
needs to stop processing. The flag would be set by whatever
error-handling code you have in situations when the service needs to
stop.
If you need to, you can introduce a counter or synchronization object
that would allow your main service thread to be signaled once all of the
threads have successfully noted that it's time to stop processing, so
that you can proceed with stopping the service.
Pete