C#终止指定线程的方法

1、创建ThreadManager类

 public class ThreadManager
    {
        private Dictionary<int, Thread> threadDict = new Dictionary<int, Thread>();
        public void AddThread(Thread thread)
        {
            int threadId = thread.ManagedThreadId;
            lock (threadDict)
            {
                if (!threadDict.ContainsKey(threadId))
                {
                    threadDict.Add(threadId, thread);
                }
            }
        }

        public void RemoveThread(Thread thread)
        {
            int threadId = thread.ManagedThreadId;
            lock (threadDict)
            {
                threadDict.Remove(threadId);
            }
        }

        public Thread GetThreadById(int threadId)
        {
            lock (threadDict)
            {
                threadDict.TryGetValue(threadId, out Thread thread);
                return thread;
            }
        }

        public IEnumerable<int> GetThreadIds()
        {
            lock (threadDict)
            {
                return threadDict.Keys;
            }
        }
    }

2、比如说现在创建了一个线程名为weight线程

  public Thread weight= new Thread(() => { });

 #region 结束线程

        public void AbortAllThreadsExceptMain()
        {
            // 创建一个线程管理器
            ThreadManager threadManager = new ThreadManager();

            // 获取主线程对象
            Thread mainThread = Thread.CurrentThread;
            int mainThreadId = mainThread.ManagedThreadId;

            // 遍历当前进程中的所有线程,将线程添加到管理器中
            foreach (ProcessThread processThread in Process.GetCurrentProcess().Threads)
            {
                int threadId = processThread.Id;
                if (threadId != mainThreadId)
                {
                    Thread thread = new Thread(() =>
                    {
                        // 线程的具体逻辑
                        Thread.Sleep(5000);
                    });
                    threadManager.AddThread(thread);
                }
            }

            // 在需要结束 weight 线程时,获取该线程对象并终止线程
            Thread weightProcessThread = threadManager.GetThreadById(weight.ManagedThreadId);
            if (weightProcessThread != null)
            {
                weightProcessThread.Abort();
                threadManager.RemoveThread(weightProcessThread);
            }

            // 在需要结束其他线程时,通过线程管理器获取线程对象并终止线程
            foreach (int threadId in threadManager.GetThreadIds())
            {
                Thread thread = threadManager.GetThreadById(threadId);
                if (thread != null && thread != weightProcessThread)
                {
                    thread.Abort();
                }
            }

            // 等待所有线程结束
            foreach (int threadId in threadManager.GetThreadIds())
            {
                Thread thread = threadManager.GetThreadById(threadId);
                thread?.Join();
            }

            Console.WriteLine("所有线程已结束");
        }
        #endregion

3、结束线程方法的使用

//检测线程是否使用
//第一种
 if (weight.IsAlive)
                {
                    try
                    {
                        AbortAllThreadsExceptMain();
                    }
                    catch (Exception)
                    {

                    }
                }
//第二种
 if (weight_process_thread.ThreadState == ThreadState.Running)
                {
                    try
                    {
                        AbortAllThreadsExceptMain();
                    }
                    catch (Exception)
                    {
                    }
                }

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky.帅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值