Old question: how to kill a thread right away in C #

I am writing a chess game that allows you to compete with two programs, the player needs to write a DLL and set a function to tell the main application in which this player will move in the next, suppose that the function looks like

public static void MoveNext(out int x, out int y, out int discKind); 

In a chess game application, I start a new thread to call a function that the DLL player has discovered to get where it will move in a turn, and I start a timer to prevent player timeouts

  private Thread mPlayerMoveThread; private void SetPlayerToMove(IPlayer player) { this.CurrentPlayer = player; try { System.Threading.Timer mTimeoutTimer = new Timer(new TimerCallback(TimeIsUp), null, this.mMaxTimeOfOnePlayer, Timeout.Infinite); mPlayerMoveThread = new Thread(this.ThreadMethodPlayerAction); mPlayerMoveThread.IsBackground = true; mPlayerMoveThread.Start(); } catch(Exception ex) { //// invalid move, so finish the game HandleInvalidMove(player.Color, ex); } } private void ThreadMethodPlayerAction() { this.CurrentPlayer.MoveNext(); } private void KillThread(Thread thread) { if (thread == null || thread.ThreadState == ThreadState.Stopped) return; try { thread.Abort(); thread.Join(); } catch { } finally { thread = null; } } private void TimeIsUp(object state) { // kill the threads KillThread(mPlayerMoveThread); HandleInvalidMove(this.CurrentPlayer.Color, new Exception("timeout")); } 

My call: Can my KillThread function correctly kill the thread? If not, what should I do to stop thead?

+1
multithreading c #
source share
3 answers

Using Thread.Abort is already bad practice because it throws a ThreadAbortedException at an arbitrary point. And it allows you to execute finally clauses. Therefore, if there is an infinite loop at the end, it will not complete the flow.

The correct way is to unload the entire AppDomain containing this stream. Upload each player dll to your own AppDomain . Thus, you can forcefully unload it whenever you want, without distorting the state of the main application.

+2
source share

Flow killing is possible, but a very poor methodology. Why not just ask your players to provide the Undo function in the DLL? You can do this, but by adding it to your interface. Abort may not stop the thread if the player catches a ThreadAbortException and does nothing with it.

0
source share

Your code will probably kill the stream, but it will not be absolutely guaranteed. For example, since the thread you are trying to kill rewinds the call stack, it will execute all finally blocks, so you depend on the code in those that don't hang. Since your question, apparently, implies that the code that runs the thread will be written by someone else, and the person writing it may well not anticipate attempts to interrupt the thread, it would be unwise for you to rely on a well-conducting thread. (It would also be unwise for you to rely on well-written code to clear all the resources that it could hold before the thread exits)

If you need an absolute 100% guarantee that you can kill a thread that calculates the move, I believe that the usual way to get this is to have a separate process host - a thread - then you can kill the process. But this assumes that you don't mind performance when you start a new process every time you need to calculate the move.

0
source share

All Articles