简体   繁体   English

我是否需要使用try catch或检查ThreadPool.QueueUserWorkItem的返回值

[英]Do I need to use try catch or check the return value of ThreadPool.QueueUserWorkItem

ThreadPool.QueueUserWorkItem ThreadPool.QueueUserWorkItem

The documentation of the method above says the following about its return type: 上面方法的文档说明了它的返回类型:

true if the method is successfully queued; 如果方法成功排队,则为true;否则为false。 NotSupportedException is thrown if the work item could not be queued. 如果工作项无法排队,则抛出NotSupportedException。

Does that mean that the function never returns false? 这是否意味着该函数永远不会返回false? But throws instead? 但反而投掷? If so, then why have a return type at all? 如果是这样,那么为什么要有返回类型呢? I would say, or return true/false whether the WorkItem has been queued, or be of type void and throw when the WorkItem couldn't be queued. 我会说,或者返回true / false,无论WorkItem是否已排队,或者当WorkItem无法排队时返回void和throw类型。

I know there is this duplicate question , but it really isn't much of definite answer in my opinion. 我知道有这个重复的问题 ,但在我看来,它确实没有多少明确的答案。

You need to read the docs carefully. 您需要仔细阅读文档。 While MSDN states: 虽然MSDN声明:

true if the method is successfully queued; 如果方法成功排队,则为true;否则为false。 NotSupportedException is thrown if the work item could not be queued. 如果工作项无法排队,则抛出NotSupportedException。

...just bellow also states: ...只是波纹管还说:

NotSupportedException: The common language runtime (CLR) is hosted, and the host does not support this action. NotSupportedException:托管公共语言运行时(CLR),主机不支持此操作。

I mean, NotSupportedException will be thrown if the hosting environment doesn't support thread pooling. 我的意思是,如果托管环境不支持线程池,则会抛出NotSupportedException

Thus, if you're queueing threads where you can do so , you don't need a try/catch block. 因此,如果你正在排队线程, 你可以这样做 ,你不需要try/catch块。 At least, you can check that the thread could be queued using the regular boolean return value. 至少,您可以使用常规布尔返回值检查线程是否可以排队。

Does that mean that the function never returns false? 这是否意味着该函数永远不会返回false? But throws instead? 但反而投掷? If so, then why have a return type at all 如果是这样,那么为什么要有返回类型

Lets look at the source code : 让我们来看看源代码

//ThreadPool has per-appdomain managed queue of work-items. The VM is
//responsible for just scheduling threads into appdomains. After that
//work-items are dispatched from the managed queue.
[System.Security.SecurityCritical]  // auto-generated
private static bool QueueUserWorkItemHelper(
    WaitCallback callBack, Object state, ref StackCrawlMark stackMark, bool compressStack)
{
    bool success =  true;

    if (callBack != null)
    {
        //The thread pool maintains a per-appdomain managed work queue.
        //New thread pool entries are added in the managed queue.
        //The VM is responsible for the actual growing/shrinking of 
        //threads. 

        EnsureVMInitialized();

        // If we are able to create the workitem, 
        // we need to get it in the queue without being interrupted
        // by a ThreadAbortException.
        //
        try { }
        finally
        {
            QueueUserWorkItemCallback tpcallBack = new QueueUserWorkItemCallback(
                                        callBack, state, compressStack, ref stackMark);
            ThreadPoolGlobals.workQueue.Enqueue(tpcallBack, true);
            success = true;
        }
    }
    else
    {
        throw new ArgumentNullException("WaitCallback");
    }
    return success;
}

It seems that success will truly either return true , as it is initially set before queuing the item, or throw an exception in case the runtime is unable to queue the delegate. 似乎成功将真正返回true ,因为它在排队项目之前初始设置,或者在运行时无法排队委托时抛出异常。

Any better ideas? 有更好的想法吗?

That seems redundant to me, unless you don't really trust the run-time environment and not sure if it supplies an underlying thread-pooling mechanism. 这对我来说似乎是多余的,除非你不真正信任运行时环境并且不确定它是否提供了底层的线程池机制。 Otherwise if you're using Microsoft's run-time, you should be good simply using the original QueueUserWorkItem . 否则,如果您使用的是Microsoft的运行时,那么您应该只使用原始的QueueUserWorkItem

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

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