473,508 Members | 1,998 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 1429
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.bigpuddle.com> wrote in message
news:%2****************@TK2MSFTNGP11.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.bigpuddle.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.com>
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.com> wrote in
news:MP************************@msnews.microsoft.c om...
Wayne M J <no*@home.nor.bigpuddle.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.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Niki Estner <ni*********@cube.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.com>
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
3266
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...
20
2990
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...
18
5834
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...
1
4454
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...
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...
6
5432
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...
23
5654
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() {...
5
5066
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 =...
6
2926
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...
0
7227
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,...
0
7391
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...
1
7054
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...
0
7501
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...
0
5633
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,...
1
5056
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...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
424
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...

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.