Skip to content

Commit 019dffa

Browse files
author
dahall
committed
Paid attention to reco on #528
1 parent 9029bb8 commit 019dffa

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

PInvoke/Shared/OverlappedAsyncBase.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public static class OverlappedAsync
3333
if (!handle.SafeWaitHandle.IsClosed)
3434
try
3535
{
36-
handle.WaitOne(millisecondsTimeout, exitContext);
36+
if (!handle.WaitOne(millisecondsTimeout, exitContext))
37+
{
38+
unsafe { CancelIoEx(result.Handle, result.Overlapped); }
39+
throw new TimeoutException("The operation has timed out.", new Win32Exception(Marshal.GetLastWin32Error()));
40+
}
3741
}
3842
finally
3943
{
@@ -114,6 +118,10 @@ private static void BindHandle(HFILE hDevice)
114118
ThreadPool.BindHandle(new Microsoft.Win32.SafeHandles.SafeFileHandle((IntPtr)hDevice, false));
115119
}
116120

121+
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
122+
[return: MarshalAs(UnmanagedType.Bool)]
123+
private static unsafe extern bool CancelIoEx([In] HFILE hFile, [In, Optional] NativeOverlapped* lpOverlapped);
124+
117125
/// <summary>Holds all pertinent information for handling results and errors in an overlapped set of method calls.</summary>
118126
/// <seealso cref="IAsyncResult"/>
119127
public sealed class OverlappedAsyncResult : IAsyncResult

0 commit comments

Comments
 (0)