有没有人知道是否有办法在Parallel.Foreach Library中指定线程等待时间?
答案 0 :(得分:1)
您可以使用Monitor
锁定并指定其超时:
// early in the method.
var obj = new object();
int timeout = 5000; // 5 seconds
bool lockWasTaken;
try
{
lockWasTaken = Monitor.TryEnter(obj, timeout);
if (lockwasTaken);
{
// your critical code
}
}
finally
{
if (lockWasTaken)
{
Monitor.Exit(obj);
}
}