I wrote the code below. I am calling this method to write to three different hard disks using three separate threads. One of the CancelIo calls is failing (returns false).
I assume that the disk is OK (the hardware is fine).
How can this be?
(After changing the disk I see it happening again on another disk.)
public void foo(byte[] bufferToWrite)
{
unsafe
{
NativeOverlapped overlapped = new NativeOverlapped()
{
EventHandle = eventHandle,
OffsetLow = ( int )( s & 0xffffffff ),
OffsetHigh = ( int )( s >> 32 & 0xffffffff )
};
GCHandle gch = GCHandle.Alloc( bufferToWrite, GCHandleType.Pinned );
IntPtr ptr = new IntPtr( ( void* )gch.AddrOfPinnedObject() );
WriteFile( handle, ptr, length, ref bytesWritten, &overlapped );
dwResult = WaitForSingleObject( EventHandle, 30000 );
if(dwResult== TIME_OUT)
{
Debug.Assert( CancelIo ( handle ) );
}
}
}