473,666 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Safest location for a Thread.Abort()

I have been using threads on and off, but I am always coming across a
situation that baffles me: where is the best place to put a Thread.Abort()
call?

I have a form, when the button event is fired, a thread is created, and it
will continue on its merry way until I force the thread to `die` via the
IDE.

I have in the past used Form_Close and Form_Closing as 2 locations for the
Thread.Abort() call, but in recent times, I have found that it does not
always work, but for the most part it works fine.

--
Wayne M Jackson
------
WWW: http://www.wjackson.cable.nu
Jul 21 '05 #1
4 1440
Hi Wayne,

The better approach would be to use some other mechanism to end the thread
(event perhaps - signal it when you want the thread to end or something like
that) as Abort has some "features".

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Wayne M J" <no*@home.nor.b igpuddle.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
I have been using threads on and off, but I am always coming across a
situation that baffles me: where is the best place to put a Thread.Abort()
call?

I have a form, when the button event is fired, a thread is created, and it
will continue on its merry way until I force the thread to `die` via the
IDE.

I have in the past used Form_Close and Form_Closing as 2 locations for the
Thread.Abort() call, but in recent times, I have found that it does not
always work, but for the most part it works fine.

--
Wayne M Jackson
------
WWW: http://www.wjackson.cable.nu

Jul 21 '05 #2
Wayne M J <no*@home.nor.b igpuddle.com> wrote:
I have been using threads on and off, but I am always coming across a
situation that baffles me: where is the best place to put a Thread.Abort()
call?

I have a form, when the button event is fired, a thread is created, and it
will continue on its merry way until I force the thread to `die` via the
IDE.

I have in the past used Form_Close and Form_Closing as 2 locations for the
Thread.Abort() call, but in recent times, I have found that it does not
always work, but for the most part it works fine.


I would personally avoid using Thread.Abort at all. Either use
background threads if you want the threads to die when the rest of the
app closes, or signal that you want the other threads to stop in a
timely manner.

See
http://www.pobox.com/~skeet/csharp/m...worker.threads
for an example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
That threading-tutorial got quite cool!

If you'd put some more details in it, and published it as a book, I'd buy
it.

Niki

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com...
Wayne M J <no*@home.nor.b igpuddle.com> wrote:
I have been using threads on and off, but I am always coming across a
situation that baffles me: where is the best place to put a Thread.Abort() call?

I have a form, when the button event is fired, a thread is created, and it will continue on its merry way until I force the thread to `die` via the
IDE.

I have in the past used Form_Close and Form_Closing as 2 locations for the Thread.Abort() call, but in recent times, I have found that it does not
always work, but for the most part it works fine.


I would personally avoid using Thread.Abort at all. Either use
background threads if you want the threads to die when the rest of the
app closes, or signal that you want the other threads to stop in a
timely manner.

See
http://www.pobox.com/~skeet/csharp/m...worker.threads
for an example.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Niki Estner <ni*********@cu be.net> wrote:
That threading-tutorial got quite cool!

If you'd put some more details in it, and published it as a book, I'd
buy it.


LOL - it's nearly as detailed as I know. I'd have to be treading in the
uneasy territory of becoming an expert to put much more into it, at
least in terms of technical details - I could write examples for ages,
of course :)

I'm just hoping I get time to actually finish it some time. I have a
sneaking suspicion that by the time I get round to writing up timers,
I'll have thought of something else which should be in there...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

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

Similar topics

7
3279
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at any point in the thread. In fact, I have a section of code needs to be "protected" from being interrupted. How can I make sure Abort() will not land anywhere winthin this block? In other words, the Abort() must wait until this block of code is done...
20
3012
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
18
5846
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this component becomes instabile, throwing a Windows like error (access violation on 0x00000002), not an framework exception. The component and all of its subcomponents are 100% managed code. What could go wrong with Thread.Abort()? Thanks for any hints.
1
4465
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am assuming that this is not an uncommon piece of software. I want to get an architecture that conforms as closely as possible with the recommendations from Microsoft on developing Windows Services, but to be honest I have found difficultly in...
4
284
by: Wayne M J | last post by:
I have been using threads on and off, but I am always coming across a situation that baffles me: where is the best place to put a Thread.Abort() call? I have a form, when the button event is fired, a thread is created, and it will continue on its merry way until I force the thread to `die` via the IDE. I have in the past used Form_Close and Form_Closing as 2 locations for the Thread.Abort() call, but in recent times, I have found that...
6
5465
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop that. This thread is also outputting strings in a RichTextBox and in some rare instances I get a System.NullReferenceException when I exit the GUI. It seems like the _Closed() Method calls Thread.Abort() and then continues closing down/disposing...
23
5695
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() { : : do stuff
5
5071
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc = proc.Start("notepad.exe");
6
2934
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread." I've had a long discussion with someone on not to use the mentioned method unless under the most extreme cases. I believe that it's
0
8454
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
8363
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
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...
1
8561
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
8645
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
7389
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
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.