By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

Thread .Aborting

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
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

Nov 16 '05 #2

P: n/a
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]
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.