示例#1
0
        /// <summary>
        /// Reading in another Thread
        /// </summary>
        private void OnReadData(object threadSleepTime)
        {
            Log("Read Thread started");

            try
            {
                int sleepTime = (int)threadSleepTime;
                while (readAsync)
                {
                    byte[] buff = new byte[REPORT_LENGTH];

                    uint             numberOfBytesRead = 0;
                    NativeOverlapped overlapped        = new NativeOverlapped();
                    overlapped.EventHandle = mre.SafeWaitHandle.DangerousGetHandle();
                    overlapped.OffsetHigh  = 0;
                    overlapped.OffsetLow   = 0;

                    bool result = HIDImports.ReadFile(this.SafeFileHandle.DangerousGetHandle(), buff, (uint)buff.Length, out numberOfBytesRead, ref overlapped);

                    Log("ReadReport: " + result.ToString());

                    if (!result)
                    {
                        uint lastError = HIDImports.GetLastError();
                        if (lastError != HIDImports.ERROR_IO_PENDING)
                        {
                            Log("Read failed: " + lastError.ToString("X"));
                            continue;
                        }
                        else
                        {
                            if (!HIDImports.GetOverlappedResult(this.SafeFileHandle.DangerousGetHandle(), ref overlapped, out numberOfBytesRead, true))
                            {
                                lastError = HIDImports.GetLastError();
                                Log("Read failed: " + lastError.ToString("X"));
                                continue;
                            }

                            if (overlapped.InternalHigh.ToInt32() == HIDImports.STATUS_PENDING || overlapped.InternalLow.ToInt32() == HIDImports.STATUS_PENDING)
                            {
                                Log("Read interrupted " + lastError.ToString("X"));
                                if (!HIDImports.CancelIo(this.SafeFileHandle.DangerousGetHandle()))
                                {
                                    lastError = HIDImports.GetLastError();
                                    Log("CancelIO failed: " + lastError.ToString("X"));
                                    continue;
                                }
                            }
                        }
                    }

                    // parse it
                    if (buff != null)
                    {
                        if (ParseInputReport(buff))
                        {
                            if (WiiControllerStateChanged != null)
                            {
                                WiiControllerStateChanged(this, new WiiControllerStateChangedEventArgs(this.WiiControllerState));
                            }
                        }
                    }

                    Thread.Sleep(sleepTime);
                }
            }
            catch (ThreadAbortException ex)
            {
                Log("Thread abort!: " + ex.Message);
            }
        }