Esempio n. 1
0
        public static unsafe ThreadPoolBoundHandle BindHandle(SafeHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            if (handle.IsClosed || handle.IsInvalid)
            {
                throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
            }

            SafeThreadPoolIOHandle threadPoolHandle = Interop.Kernel32.CreateThreadpoolIo(handle, &OnNativeIOCompleted, IntPtr.Zero, IntPtr.Zero);

            if (threadPoolHandle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)         // Bad handle
                {
                    throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
                }

                if (errorCode == Interop.Errors.ERROR_INVALID_PARAMETER)     // Handle already bound or sync handle
                {
                    throw new ArgumentException(SR.Argument_AlreadyBoundOrSyncHandle, nameof(handle));
                }

                throw Win32Marshal.GetExceptionForWin32Error(errorCode);
            }

            return(new ThreadPoolBoundHandle(handle, threadPoolHandle));
        }
Esempio n. 2
0
        public static ThreadPoolBoundHandle BindHandle(SafeHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException(nameof(handle));
            }

            if (handle.IsClosed || handle.IsInvalid)
            {
                throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
            }

            IntPtr callback = AddrofIntrinsics.AddrOf <Interop.NativeIoCompletionCallback>(OnNativeIOCompleted);
            SafeThreadPoolIOHandle threadPoolHandle = Interop.mincore.CreateThreadpoolIo(handle, callback, IntPtr.Zero, IntPtr.Zero);

            if (threadPoolHandle.IsInvalid)
            {
                int errorCode = Marshal.GetLastWin32Error();
                if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)         // Bad handle
                {
                    throw new ArgumentException(SR.Argument_InvalidHandle, nameof(handle));
                }

                if (errorCode == Interop.Errors.ERROR_INVALID_PARAMETER)     // Handle already bound or sync handle
                {
                    throw new ArgumentException(SR.Argument_AlreadyBoundOrSyncHandle, nameof(handle));
                }

                throw Win32Marshal.GetExceptionForWin32Error(errorCode);
            }

            return(new ThreadPoolBoundHandle(handle, threadPoolHandle));
        }
        public static ThreadPoolBoundHandle BindHandle(SafeHandle handle)
        {
            if (handle == null)
            {
                throw new ArgumentNullException("handle");
            }

            if (handle.IsClosed || handle.IsInvalid)
            {
                throw new ArgumentException(SR.Argument_InvalidHandle, "handle");
            }

            // Make sure we use a statically-rooted completion callback,
            // so it doesn't get collected while the I/O is in progress.
            Interop.NativeIoCompletionCallback callback = s_nativeIoCompletionCallback;
            if (callback == null)
            {
                s_nativeIoCompletionCallback = callback = new Interop.NativeIoCompletionCallback(OnNativeIOCompleted);
            }

            SafeThreadPoolIOHandle threadPoolHandle = Interop.mincore.CreateThreadpoolIo(handle, s_nativeIoCompletionCallback, IntPtr.Zero, IntPtr.Zero);

            if (threadPoolHandle.IsInvalid)
            {
                int hr = Marshal.GetHRForLastWin32Error();
                if (hr == System.HResults.E_HANDLE)         // Bad handle
                {
                    throw new ArgumentException(SR.Argument_InvalidHandle, "handle");
                }

                if (hr == System.HResults.E_INVALIDARG)     // Handle already bound or sync handle
                {
                    throw new ArgumentException(SR.Argument_AlreadyBoundOrSyncHandle, "handle");
                }

                throw Marshal.GetExceptionForHR(hr);
            }

            return(new ThreadPoolBoundHandle(handle, threadPoolHandle));
        }
Esempio n. 4
0
 private ThreadPoolBoundHandle(SafeHandle handle, SafeThreadPoolIOHandle threadPoolHandle)
 {
     _threadPoolHandle = threadPoolHandle;
     _handle           = handle;
 }
Esempio n. 5
0
 internal static unsafe partial void CancelThreadpoolIo(SafeThreadPoolIOHandle pio);
Esempio n. 6
0
 internal static extern unsafe void CancelThreadpoolIo(SafeThreadPoolIOHandle pio);
Esempio n. 7
0
 internal static extern unsafe void StartThreadpoolIo(SafeThreadPoolIOHandle pio);