HidLibrary.NativeMethods.WaitForSingleObject C# (CSharp) Method

WaitForSingleObject() private method

private WaitForSingleObject ( IntPtr hHandle, int dwMilliseconds ) : uint
hHandle System.IntPtr
dwMilliseconds int
return uint
        internal static extern uint WaitForSingleObject(IntPtr hHandle, int dwMilliseconds);

Usage Example

Example #1
0
        protected HidDeviceData ReadData(int timeout)
        {
            byte[] array = new byte[0];
            HidDeviceData.ReadStatus status = HidDeviceData.ReadStatus.NoDataRead;
            if (_deviceCapabilities.InputReportByteLength > 0)
            {
                uint lpNumberOfBytesRead = 0u;
                array = CreateInputBuffer();
                if (_deviceReadMode != DeviceMode.Overlapped)
                {
                    try
                    {
                        NativeOverlapped lpOverlapped = default(NativeOverlapped);
                        NativeMethods.ReadFile(ReadHandle, array, (uint)array.Length, out lpNumberOfBytesRead, ref lpOverlapped);
                        status = HidDeviceData.ReadStatus.Success;
                    }
                    catch
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                    }
                }
                else
                {
                    NativeMethods.SECURITY_ATTRIBUTES securityAttributes = default(NativeMethods.SECURITY_ATTRIBUTES);
                    NativeOverlapped lpOverlapped2 = default(NativeOverlapped);
                    int dwMilliseconds             = (timeout <= 0) ? 65535 : timeout;
                    securityAttributes.lpSecurityDescriptor = IntPtr.Zero;
                    securityAttributes.bInheritHandle       = true;
                    securityAttributes.nLength = Marshal.SizeOf((object)securityAttributes);
                    lpOverlapped2.OffsetLow    = 0;
                    lpOverlapped2.OffsetHigh   = 0;
                    lpOverlapped2.EventHandle  = NativeMethods.CreateEvent(ref securityAttributes, Convert.ToInt32(false), Convert.ToInt32(true), string.Empty);
                    try
                    {
                        NativeMethods.ReadFile(ReadHandle, array, (uint)array.Length, out lpNumberOfBytesRead, ref lpOverlapped2);
                        switch (NativeMethods.WaitForSingleObject(lpOverlapped2.EventHandle, dwMilliseconds))
                        {
                        case 0u:
                            status = HidDeviceData.ReadStatus.Success;
                            break;

                        case 258u:
                            status = HidDeviceData.ReadStatus.WaitTimedOut;
                            array  = new byte[0];
                            break;

                        case uint.MaxValue:
                            status = HidDeviceData.ReadStatus.WaitFail;
                            array  = new byte[0];
                            break;

                        default:
                            status = HidDeviceData.ReadStatus.NoDataRead;
                            array  = new byte[0];
                            break;
                        }
                    }
                    catch
                    {
                        status = HidDeviceData.ReadStatus.ReadError;
                    }
                    finally
                    {
                        CloseDeviceIO(lpOverlapped2.EventHandle);
                    }
                }
            }
            return(new HidDeviceData(array, status));
        }
All Usage Examples Of HidLibrary.NativeMethods::WaitForSingleObject