473,569 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread termination

I would like to execute some code when a thread terminates, in the
context of that thread - I guess similar to an ExitThread handler - is
it possible in a C# .NET application?

I can see a way to execute code when a thread is thrown away - by
having per-thread static attributes that are themselves objects, when
the thread object associated with that static value is garbage
collected, the destructor for the object is driven - but that happens
on another thread, I need the execution to happen on the thread that
is ending (because I'm going to make a call to unmanaged code to clear
up some things which the unmanaged code has associated with the
thread)...

I would like to believe that the framework does provide for a 'thread
is about to be deleted' class, which implements some interface - but I
cannot find it - unless IDisposable does it?
Nov 22 '05 #1
2 1754
ice88 <ia*********@nt lworld.com> wrote:
I would like to execute some code when a thread terminates, in the
context of that thread - I guess similar to an ExitThread handler - is
it possible in a C# .NET application?

I can see a way to execute code when a thread is thrown away - by
having per-thread static attributes that are themselves objects, when
the thread object associated with that static value is garbage
collected, the destructor for the object is driven - but that happens
on another thread, I need the execution to happen on the thread that
is ending (because I'm going to make a call to unmanaged code to clear
up some things which the unmanaged code has associated with the
thread)...

I would like to believe that the framework does provide for a 'thread
is about to be deleted' class, which implements some interface - but I
cannot find it - unless IDisposable does it?


It strikes me that the easiest way of doing this in a general way would
be something like:

public delegate void ThreadFinish();

public class ThreadStarterWi thFinish
{
public event ThreadFinish Finish;

ThreadStart starter;

public ThreadStarterWi thFinish (ThreadStart starter)
{
this.starter = starter;
}

public void Run()
{
try
{
starter();
}
finally
{
if (Finish != null)
{
Finish();
}
}
}

public void StartThread()
{
new Thread (new ThreadStart(Run )).Start();
}
}

Here's a test program to demonstrate its use:

public class Test
{
static void Main()
{
ThreadStarterWi thFinish tswf = new
ThreadStarterWi thFinish (new ThreadStart(Nor mal));

tswf.Finish += new ThreadFinish (OnFinish);

tswf.StartThrea d();
}

static void Normal()
{
Console.WriteLi ne ("Normal");
}

static void OnFinish()
{
Console.WriteLi ne ("OnFinish") ;
}
}

For a non-general solution, just put a try/finally block in the
appropriate ThreadStart-compatible method.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
ia*********@ntl world.com (ice88) wrote in message news:<59******* *************** ****@posting.go ogle.com>...
I would like to execute some code when a thread terminates, in the
context of that thread - I guess similar to an ExitThread handler - is
it possible in a C# .NET application?

I can see a way to execute code when a thread is thrown away - by
having per-thread static attributes that are themselves objects, when
the thread object associated with that static value is garbage
collected, the destructor for the object is driven - but that happens
on another thread, I need the execution to happen on the thread that
is ending (because I'm going to make a call to unmanaged code to clear
up some things which the unmanaged code has associated with the
thread)...

I would like to believe that the framework does provide for a 'thread
is about to be deleted' class, which implements some interface - but I
cannot find it - unless IDisposable does it?


OK - one more thing - someone suggested modifying the thread itself and
I see that's a good idea - but I missed one important part of this...
I don't create the thread in question. The thread creates an object
that I provide, at that point, I make calls to unmanaged code which
expect some per-thread initialization to have taken place - that same
unmanaged code expects me to do some per-thread termination when the
thread ends (and I know I won't need to call the unmanaged code any
more) - so, I am not in control of the thread.

What I think I need is an 'add this static method to the end of a
list of methods that get executed just before the thread ends' - but
I cannot create a custom thread.

The sort of processing I'm looking for is kinned to DLL_THREAD_DETA CH
processing (Dyed in the wool Win32!!)
Nov 22 '05 #3

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

Similar topics

12
18864
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering if there is a simple way from a main python program to kill a running thread? I see with the 'threading' module the way daemonic threads behave...
2
309
by: ice88 | last post by:
I would like to execute some code when a thread terminates, in the context of that thread - I guess similar to an ExitThread handler - is it possible in a C# .NET application? I can see a way to execute code when a thread is thrown away - by having per-thread static attributes that are themselves objects, when the thread object associated...
20
2998
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...
10
1900
by: Fernando Rodríguez | last post by:
Hi, Assume I have Main function that looks like this: static void Main(string args) { Action h1 = new Action(0);
7
2673
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
0
1192
by: Sir Spamallot | last post by:
Hi there, Previously when handling the termination of threads I had just called the abort method and caught it in a try catch block in the thread callback. After recently learning that this was a bad way of handling thread termination I am working on terminating threads by using ManualResetEvent's. I have come up with the following code...
3
6157
by: Raj Wall | last post by:
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 ThreadAbortException 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?
1
1738
by: Sanjay | last post by:
Hi All Have an app. On start up it creates a thread and goes to sleep. Either on termination or completion of the worker thread, it triggers the main app to start executing. Question Why is the completion of the worker/child thread triggering the execution of the main app thread. Shouldn't the main app thread remain
5
10569
by: care02 | last post by:
Hi! I have the following problem: I have written a short Python server that creates an indefinite simulation thread that I want to kill when quitting (Ctrl-C) from Python. Googling around has not given me any hints on how to cleanly kill running threads before exiting. Any help is appreciated! Carl
2
3740
by: Mike | last post by:
Hello, Ok I have 2 classes in my project, one is the main form and one is a connection class, at a certain event on my main form a new instance is made of the connection class, and a reference to the main form is passed to its constructor. The connection class opens up a new thread and starts doing work in it, and adds collected data to...
0
7694
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...
0
7921
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. ...
0
8118
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...
1
7666
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...
1
5504
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...
0
5217
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...
0
3651
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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

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.