private unsafe void VerifyHandleIsSync()
{
Debug.Assert(!_useAsyncIO);
// Do NOT use this method on pipes. Reading or writing to a pipe may
// cause an app to block incorrectly, introducing a deadlock (depending
// on whether a write will wake up an already-blocked thread or this
// Win32FileStream's thread).
Debug.Assert(Interop.Kernel32.GetFileType(_fileHandle) != Interop.Kernel32.FileTypes.FILE_TYPE_PIPE);
byte* bytes = stackalloc byte[1];
int numBytesReadWritten;
int r = -1;
// If the handle is a pipe, ReadFile will block until there
// has been a write on the other end. We'll just have to deal with it,
// For the read end of a pipe, you can mess up and
// accidentally read synchronously from an async pipe.
if ((_access & FileAccess.Read) != 0) // don't use the virtual CanRead or CanWrite, as this may be used in the ctor
{
r = Interop.Kernel32.ReadFile(_fileHandle, bytes, 0, out numBytesReadWritten, IntPtr.Zero);
}
else if ((_access & FileAccess.Write) != 0) // don't use the virtual CanRead or CanWrite, as this may be used in the ctor
{
r = Interop.Kernel32.WriteFile(_fileHandle, bytes, 0, out numBytesReadWritten, IntPtr.Zero);
}
if (r == 0)
{
int errorCode = GetLastWin32ErrorAndDisposeHandleIfInvalid(throwIfInvalidHandle: true);
if (errorCode == ERROR_INVALID_PARAMETER)
throw new ArgumentException(SR.Arg_HandleNotSync, "handle");
}
}