I'm making a game where i have several threads, one mainthread that renders the game. Then i've added one for collision-detection, one for mousemovements and one for keyboardstrokes.
The problem is that i want to close my game by hitting the escapebutton but since keyboardstrokes is handled in the keyboardthread i can't shut down the main thread. How can i do this.
Right now the shutdownfunction looks like this:
if (state[Key.Escape])
{
mouseThread.Abort();
//here i want something like: mainThread.Abort();
keyboardThread.Abort();
}
I can of course create a new thread for rendering etc but there has to be a way to close the first thread in a program.

Killing main thread from another thread?
SonosDataGuy
You should not use the Thread.Abort() method. This will potentially cause any sync objects or locked resources to be leaked. Abort is a last-resort method. Instead you should modify your threads to check for a terminate event. When the event is raised (by your keyboard handler) then the thread should terminate. When the main thread terminates the application will terminate. If a thread is marked as a background thread then it will automatically terminate when the main thread terminates.
if (state[Key.Escape])
{
m_evtTerminate.Set();
return; //Thread goes away here
}
//In other threads
while (true)
{
//Check terminate event
if (m_evtTerminate.WaitOne(0, false))
{
//Clean up resources
return; //Terminate thread
}
//Do work...
}
Michael Taylor - 12/17/05