问题
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