Thread .Aborting

X

Xarky

Hi,
I am writing a small program, that makes use of threads. Now in on
of the threads I have a critical section, where I am using the Monitor
to handle this.

*** Thread_1 *** started
for(...)
{
....doing some work
Monitor.Enter(Thread_1);
....doing a critical job
Monitor.Exit(Thread_1);
....doing some work
}

*** Another Thread....when pressing a button,
Thread1.Abort();

While Thread_1 is in the critical section, and I press the button to
abort, in some circumstances, the critical section is being interupted
without being finished.

Does the Thread.Abort() have that behaviour. If yes how can I get
around this problem?


Thanks in Advance
 
D

Dan Bass

Abort terminates the current thread where it is as far as i know. You should
perhaps have a "isRunning" variable that the threads check against to see if
they are required to run or exit. This way you can determine when they are
able to exit.

Secondly, I'd not use Monitor but lock on a local object.

As a suggestion for multi-threading, read Jon Skeets article about it:
http://www.yoda.arachsys.com/csharp/threads/

Hope that helps.

Dan.
 
R

Richard Blewett [DevelopMentor]

You can get around it by not using Thread.Abort.

You should never call Thread.Abort unless you are aborting your current thread or taking down the AppDomain - you have just witnessed some of its evils. Even using try ... finally (or lock() which is equivalent) is no guarantee of correctness.

First make sure you wrap the monitor access in a try finally or use the lock keyword which does that for you. Even better use Ian Griffith's Timedlock class [1] which allows the waiting thread to timeout rather than potentially get stuck in an unrecoverable deadlock.

Secondly use a boolean flag to indicate the thread should terminate and if the thread is blocking then call Thread.Interrupt to wake it up

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,
I am writing a small program, that makes use of threads. Now in on
of the threads I have a critical section, where I am using the Monitor
to handle this.

*** Thread_1 *** started
for(...)
{
....doing some work
Monitor.Enter(Thread_1);
....doing a critical job
Monitor.Exit(Thread_1);
....doing some work
}

*** Another Thread....when pressing a button,
Thread1.Abort();

While Thread_1 is in the critical section, and I press the button to
abort, in some circumstances, the critical section is being interupted
without being finished.

Does the Thread.Abort() have that behaviour. If yes how can I get
around this problem?


Thanks in Advance

--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.6.4 - Release Date: 07/03/2005



[microsoft.public.dotnet.languages.csharp]
 

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


Top