Mercurial > hg > ucis.core
diff Windows/WindowsNamedPipe.cs @ 111:df53bdd49507 default tip
Merge
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 07 Nov 2014 18:37:39 +0100 |
parents | 5e717aac4c1d |
children |
line wrap: on
line diff
--- a/Windows/WindowsNamedPipe.cs Mon Nov 03 18:29:58 2014 +0100 +++ b/Windows/WindowsNamedPipe.cs Fri Nov 07 18:37:39 2014 +0100 @@ -28,6 +28,9 @@ static extern unsafe bool ConnectNamedPipe(SafeFileHandle hNamedPipe, NativeOverlapped* lpOverlapped); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern unsafe bool GetNamedPipeInfo(SafeFileHandle hNamedPipe, out UInt32 lpFlags, IntPtr lpOutBufferSize, IntPtr lpInBufferSize, IntPtr lpMaxInstances); + [DllImport("kernel32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool CancelIo(SafeFileHandle hFile); const UInt32 PIPE_ACCESS_DUPLEX = 0x00000003; const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000; @@ -60,6 +63,9 @@ } public unsafe void WaitForClient() { + WaitForClient(-1); + } + public unsafe void WaitForClient(int millisecondsTimeout) { uint nread; using (ManualResetEvent evt = new ManualResetEvent(false)) { NativeOverlapped overlapped = new NativeOverlapped(); @@ -67,7 +73,10 @@ if (!ConnectNamedPipe(PipeHandle, &overlapped)) { int err = Marshal.GetLastWin32Error(); if (err != 997) throw new Win32Exception(err); - evt.WaitOne(); + if (!evt.WaitOne(millisecondsTimeout)) { + CancelIo(PipeHandle); + throw new TimeoutException("The operation timed out"); + } if (!GetOverlappedResult(PipeHandle, &overlapped, out nread, false)) throw new Win32Exception(Marshal.GetLastWin32Error()); } }