G
gabest
I've got the following situation, simplified example of course. The
thread would be deep behind interfaces, classes, etc. Is there any way
to automagically exit it without having to add an Exit() call to
everywhere? I just want to break that loop when the application is
done (the thread is still running so it cannot be done, but I think
there is no more reference from the root object, so it could call the
destructor at least), or when the object containing this thread is
collected by the GC. Apparently, it never happens now.
class Program
{
static void Main(string[] args)
{
Program p = new Program();
}
private volatile bool _exit;
public Program()
{
_exit = false;
Thread _thread = new Thread(ThreadProc);
_thread.Start();
}
~Program()
{
_exit = true;
}
void ThreadProc()
{
while(!_exit)
{
Thread.Sleep(1);
}
}
}
thread would be deep behind interfaces, classes, etc. Is there any way
to automagically exit it without having to add an Exit() call to
everywhere? I just want to break that loop when the application is
done (the thread is still running so it cannot be done, but I think
there is no more reference from the root object, so it could call the
destructor at least), or when the object containing this thread is
collected by the GC. Apparently, it never happens now.
class Program
{
static void Main(string[] args)
{
Program p = new Program();
}
private volatile bool _exit;
public Program()
{
_exit = false;
Thread _thread = new Thread(ThreadProc);
_thread.Start();
}
~Program()
{
_exit = true;
}
void ThreadProc()
{
while(!_exit)
{
Thread.Sleep(1);
}
}
}