Thread termination

  • Thread starter Thread starter ice88
  • Start date Start date
I

ice88

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?
 
ice88 said:
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 ThreadStarterWithFinish
{
public event ThreadFinish Finish;

ThreadStart starter;

public ThreadStarterWithFinish (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()
{
ThreadStarterWithFinish tswf = new
ThreadStarterWithFinish (new ThreadStart(Normal));

tswf.Finish += new ThreadFinish (OnFinish);

tswf.StartThread();
}

static void Normal()
{
Console.WriteLine ("Normal");
}

static void OnFinish()
{
Console.WriteLine ("OnFinish");
}
}

For a non-general solution, just put a try/finally block in the
appropriate ThreadStart-compatible method.
 
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_DETACH
processing (Dyed in the wool Win32!!)
 
Back
Top