我正在使用 Reflector 仔细阅读 .Net ThreadPool 的一些源代码,当它显示以下内容时:
private static bool QueueUserWorkItemHelper(WaitCallback callBack, object state, ref StackCrawlMark stackMark, bool compressStack)
{
bool flag = true;
if (callBack == null)
{
throw new ArgumentNullException("WaitCallback");
}
EnsureVMInitialized();
if (ThreadPoolGlobals.useNewWorkerPool)
{
try
{
return flag;
}
finally
{
QueueUserWorkItemCallback callback = new QueueUserWorkItemCallback(callBack, state, compressStack, ref stackMark);
ThreadPoolGlobals.workQueue.Enqueue(callback, true);
flag = true;
}
}
// code below here removed
}
try/finally block 给我的印象是非常单一的 C#。为什么要这样写呢?如果去掉 try/finally 并将 return 移到最后有什么区别?
我了解 Reflector 的工作原理,并且这可能不是原始来源。如果您认为是这种情况,您能否提出原始来源可能是什么?
最佳答案
Microsoft 已将源代码发布到 .NET - 尽管由于浏览更方便,我仍然使用 Reflector。这是 .NET 4.0 的实际代码片段。
//
// 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;
}
关于c# - ThreadPool 中的异常代码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8527725/