unsafe void StartReceive(bool synchronously) { bool useCompletionPort; try { this.handle = this.msmqQueue.GetHandleForAsync(out useCompletionPort); } catch (MsmqException ex) { OnCompletion(ex.ErrorCode, synchronously); return; } if (null != nativeOverlapped) { Fx.Assert("---- in StartReceive"); } IntPtr nativePropertiesPointer = this.message.Pin(); nativeOverlapped = new Overlapped(0, 0, IntPtr.Zero, this).UnsafePack(onPortedCompletion, this.message.GetBuffersForAsync()); int error; try { if (useCompletionPort) { error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), this.action, nativeOverlapped, null); } else { if (onNonPortedCompletion == null) { onNonPortedCompletion = new UnsafeNativeMethods.MQReceiveCallback(OnNonPortedCompletion); } error = msmqQueue.ReceiveCoreAsync(this.handle, nativePropertiesPointer, this.timeoutHelper.RemainingTime(), this.action, nativeOverlapped, onNonPortedCompletion); } } catch (ObjectDisposedException ex) { // if Close ----s with the async Receive, it is possible that SafeHandle will throw ObjectDisposedException // the behavior should be same as if operation was just cancelled (the channel will return no message) MsmqDiagnostics.ExpectedException(ex); error = UnsafeNativeMethods.MQ_ERROR_OPERATION_CANCELLED; } if (error != 0) { if (error != UnsafeNativeMethods.MQ_INFORMATION_OPERATION_PENDING) { Overlapped.Free(nativeOverlapped); nativeOverlapped = null; GC.SuppressFinalize(this); OnCompletion(error, synchronously); } } }