473,587 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(T hread_1);
....doing a critical job
Monitor.Exit(Th read_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 2027
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*********@ya hoo.com> wrote in message
news:bc******** *************** **@posting.goog le.com...
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(T hread_1);
....doing a critical job
Monitor.Exit(Th read_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.Interrup t 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(T hread_1);
....doing a critical job
Monitor.Exit(Th read_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.publi c.dotnet.langua ges.csharp]
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2355
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 thread from another, and I'm almost at the point of acquiescing but curiosity leads me on. Below is a procedure template that I wonder might guarantee behavior during an abort, and I'd like to get some feedback in case it's just plain nonsense.
4
7362
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 I suspend and resume whenver needed. In order to properly shut down the program every thread has to be aborted. So, I override OnClosing(CancelEventArgs e) in my main GUI program, and have it perform the following on the thread: if...
1
2615
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 the expected behavior for this scenario? Does the thread stay alive until the callback is executed? If an exception is thrown, can it be caught? I posted this message last week, but got no response. I am really hoping someone can help me with...
16
10072
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 ThreadAbortException is thrown, does the childThread finish the remaining code in the finally block?
4
1970
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 place (buffer) with "n" capacity. The user provides these parameters on startup. The buffer works with a circular queue. It's a console application. My implementation is working with Semaphores to synchornize everything. I think it's synchornized...
2
2488
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 a form that uses SqlDataAdapter. It fires off a thread to fill a DataSet. Not a big deal, this works. I've also got a requirement that the Thread that's going off to do the work -- if it takes too long, it has to
4
11101
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, how can I do? Thanks a lot!
4
15876
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 gets stuck (I'm working on why that happens) and I want to kill it. I do a thread.abort and the status becomes abortrequested but never actually goes through. If it were an actual process I could kill it but there seems to be no real kill for...
20
5054
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 threads" (see docs for threading.Thread objects). This will make the application terminate if only "daemon threads" are left. So best would probably be soemthing like
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.