Re: events and backgroundworker thread



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" <dvs_bis@xxxxxxxxxxxxx> wrote in message
news:CgxIj.19386$5K1.9515@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.t8x8slu28jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Tue, 01 Apr 2008 12:56:30 -0700, Analizer1 <dvs_bis@xxxxxxxxxxxxx>
wrote:

[...]
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




.



Relevant Pages

  • Re: best beginner experience
    ... manager some day. ... school student working at the Burger Chef at Tyson's Corner. ... Two more jobs you're qualified for. ... with rotting away, and whether your excuses for why you are rotting ...
    (rec.motorcycles)
  • Re: Regular Expression to Replace UPPER Case Text with lower case text
    ... advertised over a dozen times because the hiring manager was ... there are more Java jobs than, say, Python jobs, and more Python jobs ... are not idiots, they're simply ignorant. ... not engineering or technical experience. ...
    (comp.lang.perl.misc)
  • Re: prevent users from running system statistic tools
    ... > routines to see how their jobs were running. ... > I considered a script that would issue a talk command to the user. ... Have your manager have a chat with their ...
    (comp.unix.solaris)
  • Re: Limited ;) marketing idea
    ... >> license could increase the market share. ... Errm, sorry, but the personal edition is not really a good advertising. ... tell about the strength of a tool, because the manager might not know it. ... And many programmers do some private jobs beside their main ...
    (borland.public.delphi.non-technical)
  • Flags in emails - how to get date/time to print consistently when ticked "completed", pls?
    ... I use flags in email messages to help track BFs. ... dozens of emails a day and it's been very tricky trying to keep on top ... then print a hard copy of that final email with the flag and blurb. ... In last 2 jobs they haven't. ...
    (microsoft.public.outlook.general)