473,326 Members | 2,147 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,326 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 1997
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.