Stopping threas when service shutsdown

J

JSheble

I have a service that upon startup it creates two threads:

this.Batch = new ServiceThread();
this.Batch.FileSpec = "2*.XML";
this.tBatch = new Thread(new ThreadStart(this.Batch.WatchDir));
this.tBatch.Start();

this.AsNeeded = new ServiceThread();
this.AsNeeded.FileSpec = "1*.XML";
this.tAsNeeded = new Thread(new ThreadStart(this.AsNeeded.WatchDir));
this.tAsNeeded.Start();

The WatchDir method of the ServiceThread object sits in a loop, processing
files as they're dropped into a folder. (I know that most people would use
some kind of file notifier, but this is the chosen method of our design
group).

When the service shuts down, I was initially aborting the threads:

this.tBatch.Abort();
this.tAsNeeded.Abort();

However, this stops the thread no matter where in the process of the
WatchDir is currently at, which means if it's processing an actual file, it
aborts before it's actually done. How can I terminate the thread only after
it's actually done processing something without jeopordizing the serive
shutdown.

The main loop of the WatchDir method is merely a boolean check:

while(this.blIsRunning)
{
// do actual stuff here
}

The blIsRunning is a private field. I tried making this a public property
of the ServieThread class, then in the shutdown setting it to false, but the
service still terminates before the current transaction is completed...
 
W

Willy Denoyette [MVP]

JSheble said:
I have a service that upon startup it creates two threads:

this.Batch = new ServiceThread();
this.Batch.FileSpec = "2*.XML";
this.tBatch = new Thread(new ThreadStart(this.Batch.WatchDir));
this.tBatch.Start();

this.AsNeeded = new ServiceThread();
this.AsNeeded.FileSpec = "1*.XML";
this.tAsNeeded = new Thread(new ThreadStart(this.AsNeeded.WatchDir));
this.tAsNeeded.Start();

The WatchDir method of the ServiceThread object sits in a loop, processing
files as they're dropped into a folder. (I know that most people would
use some kind of file notifier, but this is the chosen method of our
design group).

When the service shuts down, I was initially aborting the threads:

this.tBatch.Abort();
this.tAsNeeded.Abort();

However, this stops the thread no matter where in the process of the
WatchDir is currently at, which means if it's processing an actual file,
it aborts before it's actually done. How can I terminate the thread only
after it's actually done processing something without jeopordizing the
serive shutdown.

The main loop of the WatchDir method is merely a boolean check:

while(this.blIsRunning)
{
// do actual stuff here
}

The blIsRunning is a private field. I tried making this a public property
of the ServieThread class, then in the shutdown setting it to false, but
the service still terminates before the current transaction is
completed...

But do you actually wait until the thransaction has finished?
Please post the code in OnStop or what you call "the shutdown".


Willy.
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

Take a look at System.Threading.WaitHandle abstract class and its
inheritors.....
 
J

JSheble

That's my question, how can I get it to not shutdown until the transaction
is finished. And I did post the code to the OnStop event, it was:

this.tBatch.Abort();
this.tAsNeeded.Abort();

These are the only lines of coed in the OnStop event. Hence my question of
asking how to accurately do it...
 
J

JSheble

I dunno if this was the correct method, but using the Join property of my
service thread seems to have done the trick...
 
W

Willy Denoyette [MVP]

JSheble said:
That's my question, how can I get it to not shutdown until the transaction
is finished. And I did post the code to the OnStop event, it was:

this.tBatch.Abort();
this.tAsNeeded.Abort();

These are the only lines of coed in the OnStop event. Hence my question
of asking how to accurately do it...

As soon as you return from OnStop the process is terminated and as such all
the threads in the process.
What you have to do is wait for the thread(s) to finish, for instance by
calling Thread.Join, but keep in mind that you need to return within 30
sec's.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

My service won't stop??? 1

Top