我写了下面的代码。我正在调用此方法以使用三个单独的线程写入三个不同的硬盘。CancelIo 调用之一失败(返回 false)。
我假设磁盘没问题(硬件没问题)。
怎么会这样?
(更换磁盘后,我看到它在另一个磁盘上再次发生。)
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 ) );
}
}
}