无法中止/取消同步/杀死后台线程

问题描述:

我们正在创建数千个后台线程,也正在特定时间段后中止/杀死它们

We are creating thousands of background threads, also we are aborting/killing them after particular span of time

最后,我们正在检查所有后台线程是否仍在运行,并通过中止/杀死它们来强制尝试摆脱它们

Finally we are checking what all background threads are still alive and forcefully trying to get rid of them by aborting/killing them

下面是我们正在努力中止/处置后台线程的代码段

Below is the code snippet where we are struggling to abort/dispose background thread

此外,我们已经成功关闭了几乎所有的后台线程,但是其中的一些线程并没有被杀死.

Also, we are successfully closing almost all the background threads however few of them are not getting killed.

先谢谢您

如果(ThreadNames.Count> 0)
                    {
               b foreach(线程名称中的变量项)
               b {
              nbsp; bsp   如果(DateTime.Now> item.Value)
              nbsp; bsp    {
              nbsp; bsp       ThreadNames.Remove(item.Key);
              nbsp; bsp       item.Key.CancelAsync();
              nbsp; bsp       item.Key.Dispose();
              nbsp; bsp    }
               b }
                    }

if (ThreadNames.Count > 0)
                    {
                        foreach (var item in ThreadNames)
                        {
                            if (DateTime.Now > item.Value)
                            {
                                ThreadNames.Remove(item.Key);
                                item.Key.CancelAsync();
                                item.Key.Dispose();
                            }
                        }
                    }

我们正在创建数千个后台线程,也在特定时间段后中止/杀死它们

We are creating thousands of background threads, also we are aborting/killing them after particular span of time

最后,我们正在检查所有后台线程是否仍在运行,并通过中止/杀死它们来强制尝试摆脱它们

Finally we are checking what all background threads are still alive and forcefully trying to get rid of them by aborting/killing them

如果我是该项目的项目经理,我会将设计该项目的程序员/分析师送回补习学校.

If I was the project manager on this project, I would send the programmer/analyst who designed this project back to remedial school.

创建数千个后台线程是很疯狂的.由于活动线程的数量仅限于计算机中的内核数量,因此它不必要地占用资源.此外,在线程超过时间限制时终止线程强烈表明性能不佳 考虑设计.通常,应该在停止线程时发信号通知线程,以便可以适当地清理资源.

Creating thousands of background threads is crazy. It sucks up resources needlessly since the number of active threads is limited to the number of cores in your computer. Moreover, killing threads when they exceed a time limit strongly suggests a poorly thought out design. Typically, one should be signalling a thread when it is time for it is to stop so that proper cleanup of resources can take place.

我强烈建议您考虑进行彻底的重新设计

I strongly urge you to consider a complete re-design