我是否需要使用 try catch 或检查 ThreadPool.QueueUserWorkItem 的 return 值
Do I need to use try catch or check the return value of ThreadPool.QueueUserWorkItem
上述方法的文档对其 return 类型说明如下:
true if the method is successfully queued; NotSupportedException is thrown if the work item could not be queued.
这是否意味着该函数永远不会 return 为假?但是抛出呢?如果是这样,那为什么要有 return 类型呢?我会说,或 return true/false WorkItem 是否已排队,或者类型为 void 并在 WorkItem 无法排队时抛出。
我知道有这个duplicate question,但在我看来它确实不是很确定的答案。
您需要仔细阅读文档。虽然 MSDN 指出:
true if the method is successfully queued; NotSupportedException is
thrown if the work item could not be queued.
...下面还说:
NotSupportedException: The common language runtime (CLR) is hosted,
and the host does not support this action.
我的意思是,如果托管环境不支持线程池,将抛出 NotSupportedException
。
因此,如果您在 可以 的地方对线程进行排队,则不需要 try/catch
块。至少,您可以使用常规布尔值 return 检查线程是否可以排队。
Does that mean that the function never returns false? But throws
instead? If so, then why have a return type at all
//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;
}
似乎成功将真正 return true
,因为它最初是在排队项目之前设置的,或者在 运行time 无法排队的情况下抛出异常代表。
Any better ideas?
这对我来说似乎是多余的,除非您真的不信任 运行-time 环境并且不确定它是否提供底层线程池机制。否则,如果您使用的是 Microsoft 的 运行-time,则只需使用原始的 QueueUserWorkItem
.
就可以了
上述方法的文档对其 return 类型说明如下:
true if the method is successfully queued; NotSupportedException is thrown if the work item could not be queued.
这是否意味着该函数永远不会 return 为假?但是抛出呢?如果是这样,那为什么要有 return 类型呢?我会说,或 return true/false WorkItem 是否已排队,或者类型为 void 并在 WorkItem 无法排队时抛出。
我知道有这个duplicate question,但在我看来它确实不是很确定的答案。
您需要仔细阅读文档。虽然 MSDN 指出:
true if the method is successfully queued; NotSupportedException is thrown if the work item could not be queued.
...下面还说:
NotSupportedException: The common language runtime (CLR) is hosted, and the host does not support this action.
我的意思是,如果托管环境不支持线程池,将抛出 NotSupportedException
。
因此,如果您在 可以 的地方对线程进行排队,则不需要 try/catch
块。至少,您可以使用常规布尔值 return 检查线程是否可以排队。
Does that mean that the function never returns false? But throws instead? If so, then why have a return type at all
//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;
}
似乎成功将真正 return true
,因为它最初是在排队项目之前设置的,或者在 运行time 无法排队的情况下抛出异常代表。
Any better ideas?
这对我来说似乎是多余的,除非您真的不信任 运行-time 环境并且不确定它是否提供底层线程池机制。否则,如果您使用的是 Microsoft 的 运行-time,则只需使用原始的 QueueUserWorkItem
.