C#(Xamarin)WaitHandle.WaitAny返回Int32.MaxValue(0x7FFF FFFF)

时间:2017-03-10 21:19:24

标签: c# asynchronous handle waithandle

WaitHandle.WaitAny方法(WaitHandle [],Int32)的文档说它返回"满足等待的对象的数组索引,如果没有对象满足等待的WaitTimeout和等于millisecondsTimeout的时间间隔已过了"在程序集浏览器中,我看到WaitHandle.WaitTimeout是258。

那么为什么我有时会将0x7fffffff(Int32.MaxValue)作为此函数的返回值?

这是WaitAny()中的已知错误,还是文档中缺少的一行?

后来添加:

顺便说一句,错误发生了几次,然后就消失了,没有我做任何事情来修复"手柄或类似的东西。

Microsoft Documentation

Xamarin Documentation

代码:

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") + ")!");
            }
        }
    }
}

0 个答案:

没有答案