473,725 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread Clean up

Hi,

I have an application that uses a number of sub-threads.

What is the best way to do some processing in each thread when the main
application is shut down?
Is the ThreadAbortExce ption thrown automatically for each thread? Or is
there some other event or exception automatically thrown that the thread can
"grab" as it is shut down?

Thank-you for your help!

Regards,
Raj
Jun 6 '06 #1
3 6173
Hi Raj,

Thank you for posting.

When a call is made to the Abort method to destroy a thread, the common
language runtime throws a ThreadAbortExce ption. ThreadAbortExce ption is a
special exception that can be caught. When dealting with the
ThreadAbortExce ption, you could use Thread.ResetAbo rt to cancel the abort
and go on executing the rest code. And you may call the Thread.Join method
if you want to wait until the aborted thread has ended. Join is a blocking
call that does not return until the thread actually stops executing.

The following is a sample on how to dealt with the termination of thread.

public class ThreadWork {
public static void DoWork() {
try {
for(int i=0; i<100; i++) {
Console.WriteLi ne("Thread - working.");
Thread.Sleep(10 0);
}
}
catch(ThreadAbo rtException e) {
Console.WriteLi ne("Thread - caught ThreadAbortExce ption -
resetting.");
Console.WriteLi ne("Exception message: {0}", e.Message);
Thread.ResetAbo rt();
}
Console.WriteLi ne("Thread - still alive and working.");
Thread.Sleep(10 00);
Console.WriteLi ne("Thread - finished working.");
}
}

class ThreadAbortTest {
public static void Main() {
ThreadStart myThreadDelegat e = new ThreadStart(Thr eadWork.DoWork) ;
Thread myThread = new Thread(myThread Delegate);
myThread.Start( );
Thread.Sleep(10 0);
Console.WriteLi ne("Main - aborting my thread.");
myThread.Abort( );
myThread.Join() ;
Console.WriteLi ne("Main ending.");
}
}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =======
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
=============== =============== =============== =======

Jun 7 '06 #2
Linda, hi,

Thanks for the reply.

If I understand you, when my main program starts shutting down, I should
foreach loop thru my set of threads and send each an Abort message? (and
then set up to catch in the thread to shut down the thread properly).

Thanks again for your help!

Regards,
Raj
"Linda Liu [MSFT]" <v-****@online.mic rosoft.com> wrote in message
news:eU******** ******@TK2MSFTN GXA01.phx.gbl.. .
Hi Raj,

Thank you for posting.

When a call is made to the Abort method to destroy a thread, the common
language runtime throws a ThreadAbortExce ption. ThreadAbortExce ption is a
special exception that can be caught. When dealting with the
ThreadAbortExce ption, you could use Thread.ResetAbo rt to cancel the abort
and go on executing the rest code. And you may call the Thread.Join method
if you want to wait until the aborted thread has ended. Join is a blocking
call that does not return until the thread actually stops executing.

The following is a sample on how to dealt with the termination of thread.

public class ThreadWork {
public static void DoWork() {
try {
for(int i=0; i<100; i++) {
Console.WriteLi ne("Thread - working.");
Thread.Sleep(10 0);
}
}
catch(ThreadAbo rtException e) {
Console.WriteLi ne("Thread - caught ThreadAbortExce ption -
resetting.");
Console.WriteLi ne("Exception message: {0}", e.Message);
Thread.ResetAbo rt();
}
Console.WriteLi ne("Thread - still alive and working.");
Thread.Sleep(10 00);
Console.WriteLi ne("Thread - finished working.");
}
}

class ThreadAbortTest {
public static void Main() {
ThreadStart myThreadDelegat e = new ThreadStart(Thr eadWork.DoWork) ;
Thread myThread = new Thread(myThread Delegate);
myThread.Start( );
Thread.Sleep(10 0);
Console.WriteLi ne("Main - aborting my thread.");
myThread.Abort( );
myThread.Join() ;
Console.WriteLi ne("Main ending.");
}
}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't
hesitate
to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =======
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
=============== =============== =============== =======

Jun 7 '06 #3
Hi Raj,

Thank you for your response.

Yes, you are right. Before you want to shut down your program, you should
call the Abort method of the sub-threads in your program and call the Join
method of each sub-thread to wait for them stopping executing correctly.
And in the methods the sub-threads execute, you should add try and catch
block to handle with the ThreadAbortExce ption and use Thread.ResetAbo rt to
cancel the abort in the catch block.

If you have any other concerns or need anything else, please don't hesitate
to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support

=============== =============== =============== =======
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
=============== =============== =============== =======

Jun 8 '06 #4

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

Similar topics

13
3013
by: Alvin Bruney | last post by:
I have an array list of queries. The arraylist is variable, anywhere from 10 to 10000 or more. I'd like to spin threads to take chunks of 500 queries out of that array list, no more than 10 threads (context switching reasons). if it's less than 500 i spin only one thread. I am having trouble building an efficient, clean implementation of this. Clean means, i don't want to loop from 500 to 1000 if i only have 650 items, i would like to...
20
3028
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...
6
10751
by: LP | last post by:
Hi, I am starting a new thread from the main UI thread. If users clicks cancel button, the thread is aborted: workerThread.Abort(); ThreadAbort Exception is handeled in the worker thread: catch (System.Threading.ThreadAbortException ex)
18
5857
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.
9
7419
by: Li Pang | last post by:
Hi I make an app which can run some sub processes through multiple threads. I'd like to know how to terminate all sub-threads when the main thread is closed thanks in advance
7
2128
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it waits for the worker thread to complete by means of a while t.isAlive, sleep(0) mechanism. But when my worker thread calls my UpdateProgressBar routine, which calls Me.Invoke, the invoke call blocks forever. But I can't figure out why the main...
23
5707
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
0
8872
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
9392
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
9162
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
8069
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
6694
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
5997
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4505
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
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.