public void Open(string path, FileAccess mode, uint timeout)
        {
            IntPtr nativeHandle;

            for (; ;)
            {
                NativeMethods.SecurityAttributes security = new NativeMethods.SecurityAttributes();
                security.inheritHandle = true;
                security.Length        = Marshal.SizeOf(security);

                nativeHandle = NativeMethods.CreateFile(path, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
                                                        0, security, NativeMethods.OPEN_EXISTING, NativeMethods.FILE_FLAG_OVERLAPPED, 0);

                if (nativeHandle != IntPtr.Zero)
                {
                    break;
                }

                if (Marshal.GetLastWin32Error() != ERROR_PIPE_BUSY)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(),
                                             "Error opening pipe");
                }
                LowResolutionStopwatch sw = LowResolutionStopwatch.StartNew();
                bool success = NativeMethods.WaitNamedPipe(path, timeout);
                sw.Stop();
                if (!success)
                {
                    if (timeout < sw.ElapsedMilliseconds ||
                        Marshal.GetLastWin32Error() == ERROR_SEM_TIMEOUT)
                    {
                        throw new TimeoutException("Timeout waiting for named pipe");
                    }
                    else
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error(),
                                                 "Error waiting for pipe");
                    }
                }
                timeout -= (uint)sw.ElapsedMilliseconds;
            }
            handle     = new SafeFileHandle(nativeHandle, true);
            fileStream = new FileStream(handle, mode, 4096, true);
        }
    public void Open(string path, FileAccess mode, uint timeout)
    {
      IntPtr nativeHandle;

      for (; ; )
      {
        NativeMethods.SecurityAttributes security = new NativeMethods.SecurityAttributes();
        security.inheritHandle = true;
        security.Length = Marshal.SizeOf(security);

        nativeHandle = NativeMethods.CreateFile(path, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE,
                     0, security, NativeMethods.OPEN_EXISTING, NativeMethods.FILE_FLAG_OVERLAPPED, 0);

        if (nativeHandle != IntPtr.Zero)
          break;

        if (Marshal.GetLastWin32Error() != ERROR_PIPE_BUSY)
        {
          throw new Win32Exception(Marshal.GetLastWin32Error(),
              "Error opening pipe");
        }
        LowResolutionStopwatch sw = LowResolutionStopwatch.StartNew();
        bool success = NativeMethods.WaitNamedPipe(path, timeout);
        sw.Stop();
        if (!success)
        {
          if (timeout < sw.ElapsedMilliseconds ||
              Marshal.GetLastWin32Error() == ERROR_SEM_TIMEOUT)
          {
            throw new TimeoutException("Timeout waiting for named pipe");
          }
          else
          {
            throw new Win32Exception(Marshal.GetLastWin32Error(),
                "Error waiting for pipe");
          }
        }
        timeout -= (uint)sw.ElapsedMilliseconds;
      }
      handle = new SafeFileHandle(nativeHandle, true);
      fileStream = new FileStream(handle, mode, 4096, true);
    }