When ThreadPool.QueueUserWorkItem returns false

a 夏天 提交于 2019-11-28 08:10:01

问题


The MSDN states that the method returns

true if the method is successfully queued; NotSupportedException is thrown if the work item is not queued.

For testing purposes how to get the method to return false? Or it is just a "suboptimal" class design?


回答1:


In looking at the source code in Reflector, it seems the only part of the code that could return "false" is a call to the following:

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool AdjustThreadsInPool(uint QueueLength);



回答2:


This is probably a case of "reserved for future use". You may want to treat it as failure, but it'll be hard to test.

I pretty much treat this method as a void/Sub.




回答3:


It is imaginable that the entire API (thread-pools) becomes obsolete, when the Task Parallel Library (TPL) arrives.




回答4:


true if the method is successfully queued; NotSupportedException is thrown if the work item is not queued.

Treat a return false in the same way that you treat a NotSupportedException.

To get it to return false, use a mock method or object.
You want to be testing your own code that you wrote, not the underlying windows code. I'm sure microsoft have plenty of their own tests already for that.



来源:https://stackoverflow.com/questions/150645/when-threadpool-queueuserworkitem-returns-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!