コード例 #1
0
    public static void SafeWaitHandleExtensions_nullWaitHandle()
    {
        WaitHandle wh = null;

        Assert.Throws <ArgumentNullException>(() => wh.GetSafeWaitHandle());
        Assert.Throws <ArgumentNullException>(() => wh.SetSafeWaitHandle(new SafeWaitHandle(IntPtr.Zero, false)));
    }
コード例 #2
0
ファイル: Device.cs プロジェクト: zmtzawqlp/SharpDX
        // Disabled as it seems to be not used anymore
        ///// <summary>
        ///// Sends data to a device that accepts output. Applications should not use this method. Force Feedback is the recommended way to send data to a device. If you want to send other data to a device, such as changing LED or internal device states, the HID application programming interface (API) is the recommended way.
        ///// </summary>
        ///// <param name="data">An array of object data.</param>
        ///// <param name="overlay">if set to <c>true</c> the device data sent is overlaid on the previously sent device data..</param>
        ///// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
        //public Result SendData(ObjectData[] data, bool overlay)
        //{
        //    unsafe
        //    {
        //        int count = data==null?0:data.Length;
        //        return SendDeviceData(sizeof (ObjectData), data, ref count, overlay ? 1 : 0);
        //    }
        //}

        /// <summary>
        /// Specifies an event that is to be set when the device state changes. It is also used to turn off event notification.
        /// </summary>
        /// <param name="eventHandle">Handle to the event that is to be set when the device state changes. DirectInput uses the Microsoft Win32 SetEvent function on the handle when the state of the device changes. If the eventHandle parameter is null, notification is disabled.</param>
        /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns>
        public void SetNotification(WaitHandle eventHandle)
        {
            Microsoft.Win32.SafeHandles.SafeWaitHandle safeHandle;
#if NET45 || BEFORE_NET45
            safeHandle = eventHandle?.SafeWaitHandle;
#else
            safeHandle = eventHandle?.GetSafeWaitHandle();
#endif
            SetEventNotification(safeHandle?.DangerousGetHandle() ?? IntPtr.Zero);
        }
コード例 #3
0
            private static void StartHelper(NetworkAddressChangedEventHandler caller, bool captureContext, StartIPOptions startIPOptions)
            {
                lock (s_globalLock)
                {
                    // Setup changedEvent and native overlapped struct.
                    if (s_ipv4Socket == null)
                    {
                        // Sockets will be initialized by the call to OSSupportsIP*.
                        if (Socket.OSSupportsIPv4)
                        {
                            s_ipv4Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)
                            {
                                Blocking = false
                            };
                            s_ipv4WaitHandle = new AutoResetEvent(false);
                        }

                        if (Socket.OSSupportsIPv6)
                        {
                            s_ipv6Socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, 0)
                            {
                                Blocking = false
                            };
                            s_ipv6WaitHandle = new AutoResetEvent(false);
                        }
                    }

                    if (caller != null)
                    {
                        s_addressChangedSubscribers.TryAdd(caller, captureContext ? ExecutionContext.Capture() : null);
                    }

                    if (s_isListening || s_addressChangedSubscribers.Count == 0)
                    {
                        return;
                    }

                    if (!s_isPending)
                    {
                        if (Socket.OSSupportsIPv4 && (startIPOptions & StartIPOptions.StartIPv4) != 0)
                        {
                            ThreadPool.RegisterWaitForSingleObject(
                                s_ipv4WaitHandle,
                                new WaitOrTimerCallback(AddressChangedCallback),
                                StartIPOptions.StartIPv4,
                                -1,
                                true);

                            SocketError errorCode = Interop.Winsock.WSAIoctl_Blocking(
                                s_ipv4Socket.Handle,
                                (int)IOControlCode.AddressListChange,
                                null, 0, null, 0,
                                out int length,
                                IntPtr.Zero, IntPtr.Zero);

                            if (errorCode != SocketError.Success)
                            {
                                NetworkInformationException exception = new NetworkInformationException();
                                if (exception.ErrorCode != (uint)SocketError.WouldBlock)
                                {
                                    throw exception;
                                }
                            }

                            errorCode = Interop.Winsock.WSAEventSelect(
                                s_ipv4Socket.SafeHandle,
                                s_ipv4WaitHandle.GetSafeWaitHandle(),
                                Interop.Winsock.AsyncEventBits.FdAddressListChange);

                            if (errorCode != SocketError.Success)
                            {
                                throw new NetworkInformationException();
                            }
                        }

                        if (Socket.OSSupportsIPv6 && (startIPOptions & StartIPOptions.StartIPv6) != 0)
                        {
                            ThreadPool.RegisterWaitForSingleObject(
                                s_ipv6WaitHandle,
                                new WaitOrTimerCallback(AddressChangedCallback),
                                StartIPOptions.StartIPv6,
                                -1,
                                true);

                            SocketError errorCode = Interop.Winsock.WSAIoctl_Blocking(
                                s_ipv6Socket.Handle,
                                (int)IOControlCode.AddressListChange,
                                null, 0, null, 0,
                                out int length,
                                IntPtr.Zero, IntPtr.Zero);

                            if (errorCode != SocketError.Success)
                            {
                                NetworkInformationException exception = new NetworkInformationException();
                                if (exception.ErrorCode != (uint)SocketError.WouldBlock)
                                {
                                    throw exception;
                                }
                            }

                            errorCode = Interop.Winsock.WSAEventSelect(
                                s_ipv6Socket.SafeHandle,
                                s_ipv6WaitHandle.GetSafeWaitHandle(),
                                Interop.Winsock.AsyncEventBits.FdAddressListChange);

                            if (errorCode != SocketError.Success)
                            {
                                throw new NetworkInformationException();
                            }
                        }
                    }

                    s_isListening = true;
                    s_isPending   = true;
                }
            }