473,218 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Thread .Aborting

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
2 1991
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: cottonviking | last post by:
Greetings, all! I've been pondering the pitfalls of aborting a secondary thread in a service app I'm writing (VB, fx v1.1). Everything I've read so far pretty much dissuades one from aborting one...
4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
1
by: johnny | last post by:
In a multi-threaded application, say a worker thread makes an asynchronous call and specifies a callback method. But before the callback is executed, the thread is aborted by its creator. What is...
16
by: Bill | last post by:
Say I have a childThread currently is running a finally block to cleanup external resources. At the same time the main thread calls childThread.Abort(). The question is: when the...
4
by: Leonardo Hyppolito | last post by:
Hello, I am trying to write a multithread program that simulates producers and consumers. My program can have many producers and many consumers (each in a separate thread). It has a storage...
2
by: Sgt. Sausage | last post by:
New to multi-threading (less than 24 hours at it <grin>) Anyway, it's all making sense, and working fairly well thus far, but I'm having a minor issue I'm not sure how to get around. I've got...
4
by: am | last post by:
Hi to all. I have a little problem. I'm working with threads, and I need to abort or suspend them, but many experts dissuade from use Thread.Abort and Thread.Suspend. As I didn't find other way,...
4
by: Mufasa | last post by:
Is there any way to force a thread to abort and really have it abort? I have a thread that every once in a while gets hung to so I kill it. Problem is I have a thread that every once in a while...
20
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post by:
John Dohn wrote: When I do this, I put a special value in the queue (like None) and in the worker thread, check for the special value and exit if found. Threads can also be marked as "daemon...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.