简体   繁体   English

C#检查线程池和其他线程

[英]c# check of threadpool and other threads

Basically - we write unit tests. 基本上-我们编写单元测试。 Sometimes those unit tests start threads - and often start tasks on the threadpool. 有时,那些单元测试会启动线程-并经常在线程池上启动任务。 If something goes wrong in a background thread - it can causes strange problems with future tests. 如果后台线程出了点问题,可能会在以后的测试中引起奇怪的问题。 What we want to do on the base teardown of each test is basically 我们希望在每个测试的基础拆解基础上要做的基本上是

  • check what threads are running 检查正在运行的线程
  • fail the test if any are running that shouldn't be 如果正在运行的测试不应该通过测试

now, for normal threads, we can just enumerate before hand, and compare to afterwards - which is fine. 现在,对于普通线程,我们可以事先枚举,然后与之后进行比较-很好。 The threadpool messes things up - because there may have been many new threads validly created, which are just waiting around doing nothing - which is fine. 线程池将事情搞砸了-因为可能已经有效地创建了许多新线程,而这些线程只是无所事事地等待着-很好。 It's NOT fine if the test has left something running. 如果测试使某些程序无法运行,那就不好了。 Remember also - I am not writing the tests or code being tested - I'm writing the underlying libraries that try to ensure that no one else can screw things up, however hard they try - so I can't try using my own implementation of a threadpool or anything like that, because I can't be sure someone is not using the standard one. 还要记住-我不是在编写测​​试或正在测试的代码-我正在编写底层库,试图确保没有其他人可以搞砸事情,不管他们努力尝试了什么-所以我不能尝试使用自己的实现线程池之类的东西,因为我不能确定有人没有使用标准线程池。

Can anyone think of a way either to tell what threads are out there owned by the threadpool, and whether they are idle? 谁能想到一种方法来告诉线程池拥有哪些线程,以及它们是否空闲? My next step is to crawl through the private variables with reflection looking - but I'm hoping someone has a better way? 我的下一步是使用反射外观来爬网私有变量-但我希望有人有更好的方法吗?

Thanks, Darren 谢谢,达伦

I infer you are using fire-and-forget style threads/tasks/workitems. 我推断您正在使用即弃型样式线程/任务/工作项。 If you weren't you would not have the problem that they continue running after the test. 如果不是,您将不会遇到问题,因为它们在测试后仍会继续运行。

I consider fire-and-forget to be a problematic pattern because even outside of unit tests (in production) you never want to forget about errors. 我认为“一劳永逸”是一个有问题的模式,因为即使在单元测试之外(在生产中),您也永远不会忘记错误。 Also, in ASP.NET you are not guaranteed that background work will ever complete because the worker process can shut down after all pending HTTP requests have been processed and before the background work is done. 另外,在ASP.NET中,不能保证后台工作将永远完成,因为在处理完所有待处理的HTTP请求之后以及完成后台工作之前,工作进程可能会关闭。

So I recommend that you audit your code and switch all concurrency to the new Task -based model. 因此,我建议您审核代码并将所有并发切换到新的基于Task的模型。 That allows you to track completion. 这样就可以跟踪完成情况。 It also allows you to wait for completion and propagate errors. 它还允许您等待完成并传播错误。

You could add all started tasks to a list (maybe using a custom TaskScheduler , maybe manually). 您可以将所有已启动的任务添加到列表中(可能使用自定义TaskScheduler ,也可能手动使用)。 When the unit test shuts down you do a Task.WaitAll on the contents of that list. 当单元测试关闭时,您对该列表的内容执行Task.WaitAll That guarantees you completion. 那保证您完成。

In any case the threadpool does not allow you to listen for enqueued items or for completion. 无论如何,线程池都不允许您监听排队的项目或完成。 You need to solve this in your own code. 您需要用自己的代码解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM