Thread.Sleep没有工作和永恒的循环

时间:2016-11-18 09:48:16

标签: c# .net multithreading thread-safety

public void Stop()
    {
        lock(_threadHandlerLock)
        {
            if(!stop)
            {
                stop = true;

                while(ThreadPool.Count > 0) // count = 1 and an eternal cycle
                {
                    Thread.Sleep(1000); // here is a problem

                }

                LoggerHandler.Info(string.Format("{0} is stopped.", this.ToolName));
            }
        }
    }

我将这个Stop()称为6个线程,前5个正常停止,但不是最后一个。拜托,有人可以帮忙吗?我该怎么做才能消除这种行为?

UPD

     public void AddThread(Thread t, string name = "")
    {
        lock(_threadPool)
        {
            t.Name = name;
            _threadPool.Add(t, name);
        }
    }


    public void RemoveThread(Thread t)
    {
        lock(_threadPool)
        {
            _threadPool.Remove(t);
        }
    }

0 个答案:

没有答案