WaitHandle.WaitAny Method (WaitHandle[], Int32) 的文档说它返回“满足等待的对象的数组索引,如果没有对象满足等待,则返回 WaitTimeout,并且时间间隔相当于毫秒超时已经过去”,并且在程序集浏览器我看到 WaitHandle.WaitTimeout 是 258。
那么为什么我有时会从这个函数中得到 0x7fffffff (Int32.MaxValue) 作为返回值呢?
这是 WaitAny() 中的已知错误,还是文档中缺少一行?
稍后添加:
顺便说一句,错误发生了几次,然后就消失了,我没有做任何事情来“修复”手柄或类似的东西。
代码:
class CommandQueue
{
EventWaitHandle m_abortRequest = new EventWaitHandle (false, EventResetMode.AutoReset);
Semaphore m_commandCount = new Semaphore (0, 10000);
public CommandQueue ()
{
m_processTask = new Task(() => Process ());
m_processTask.Start ();
}
public async Task Process()
{
WaitHandle [] handles = new WaitHandle [2];
handles [0] = m_abortRequest;
handles [1] = m_commandCount;
for (;;)
{
int wait = WaitHandle.WaitAny (handles, 500);
if (wait == WaitHandle.WaitTimeout)
{
OnIdleTimer ();
}
else if (wait == 0)
{
// SNIP: Code to handle a request to abort the processing of commands
break;
}
else if (wait == 1)
{
// SNIP: Code to process the next command in the queue
await Process(nextCommand);
}
else
{
Debug.WriteLine ("*** Unexpected WaitAny(" + wait + " = 0x" + wait.ToString("X6") + ")!");
}
}
}
}