Unstoppable thread in application end???

  • Thread starter Thread starter Gaetan
  • Start date Start date
G

Gaetan

Hi

let this code :

using System;
using System.Threading;

namespace ThisNamespace
{
class ThisClass
{
static void Main()
{
new ThisClass();
// here the end of the application => should call the
"destructor"
}

public ThisClass()
{
StartThread();
}

// finaliser
~ThisClass()
{
_WorkingThread.Abort();
}

Thread _WorkingThread;

public void StartThread()
{
_WorkingThread = new Thread( new ThreadStart(DoWork));
_WorkingThread.Start();
// if you remove this Start(), the "destructor" is well
called...
}

void DoWork()
{
while(true)
;
}
}
}

This is a simple example of what i would like to do: a class that
launch an other thread in this constructor, this thread doint whatever
it wants.
But when the application is closed, the thread should be destroyed
(properly by calling _WorkingThread.Abort in the "destructor"). But
this destructor is not called and so, I cannot stop my thread, and
this result with an executable still launched !!!

Any idea please?

PS: and please, no ThreadPool based answer...
 
Hi,

1. The destructor will be called WHEN garbage collector destroys the
ThisClass object. You can not predict it when, so maybe GC.Collect may
help.

2. Thread.Abort is not very good method for terminating a thread.

3. You can set your thread to be background just before starting it,
then it will be terminated upon application exit.

Sunny
 
Sunny said:
Hi,

1. The destructor will be called WHEN garbage collector destroys the
ThisClass object. You can not predict it when, so maybe GC.Collect may
help.

2. Thread.Abort is not very good method for terminating a thread.

3. You can set your thread to be background just before starting it,
then it will be terminated upon application exit.
Thank you very much.
So what is your advise for terminating a thread? I mean, when the
application will close, the thread should close by themself... I can
add a terminated check into the threads, but I need a way to tell the
thread it should end... how to do that? By recording all the thread
into an internal list? I hoped the destructor (ah.... i miss my lovely
c++ destructors :) ) should do that...

Gaetan
 
Hi, Gaetan

You can return from thread method body on termination check. This will
effectively terminate the thread. You can tell thread to terminate using
global flag, event or variety of other techniques. What is exactly your
problem?

HTH
Alex
 
Hi Gaetan,

Thank you very much.
So what is your advise for terminating a thread? I mean, when the
application will close, the thread should close by themself... I can
add a terminated check into the threads, but I need a way to tell the
thread it should end... how to do that? By recording all the thread
into an internal list? I hoped the destructor (ah.... i miss my lovely
c++ destructors :) ) should do that...

Gaetan

As AlexS already stated, you can make some global (or local) flag, and
in your thread you can check if the thread have to end.

using System;
using System.Threading;

namespace ThisNamespace
{
class ThisClass
{
static void Main()
{
ThisClass mythread = new ThisClass();

mythread.StopThread();
}

public ThisClass()
{
StartThread();
}


Thread _WorkingThread;
bool keepAlive;

public void StartThread()
{
_WorkingThread = new Thread( new ThreadStart(DoWork));
keepAlive = true;
_WorkingThread.Start();
}

void DoWork()
{
while(keepAlive)
;
}

public void StopThread()
{
keepAlive = false;
}
}
}

Sunny
 
Yes, I know. I just edited his example to make it work the way OP
wanted.

Thanks for the note anyway.

Sunny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top