老问题:如何立即在C#中杀死线程 [英] Old question: how to kill a thread immediately in c#

查看:138
本文介绍了老问题:如何立即在C#中杀死线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个允许两个程序竞争的象棋游戏,玩家需要编写一个DLL并公开一个函数,以告知主应用程序该玩家下一步将移动的位置,假设该函数看起来像

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

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

在国际象棋游戏应用程序中,我启动了一个新线程来调用该函数,该函数公开了玩家的DLL以获取转弯位置,并且我启动了一个计时器以防止玩家超时

In the chess game application, I start a new thread to call the function that the player's DLL exposed to get where he will move in a turn, and I start a timer to prevent the 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"));
        }

我的问题是: 我的函数 KillThread 是否可以正确杀死线程?如果没有,我该怎么做才能停止剧情?

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

推荐答案

使用Thread.Abort已经不是一种好习惯,因为它会在任意位置抛出ThreadAbortedException.并且它允许finally子句执行.因此,如果在final内部存在无限循环,它将不会终止线程.

Using Thread.Abort is already bad practice because it throws a ThreadAbortedException at an arbitrary point. And it allows finally clauses to execute. So if there is an endless loop inside a finally it won't terminate the thread.

正确的方法是卸载包含该线程的整个AppDomain.将每个播放器的dll加载到其自己的AppDomain中.这样,您可以在需要时强制卸载它,而不会破坏主应用程序的状态.

The right way is to unload the whole AppDomain containing that thread. Load each player's dll in its own AppDomain. That way you can force-unload it when you want without corrupting the state of the main application.

这篇关于老问题:如何立即在C#中杀死线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆