当长时间运行的ADO.NET进程需要等待操作完成时,可以使用CoWaitForMultipleHandles函数等待线程堵塞的句柄。以下是代码示例:
using System.Runtime.InteropServices;
public void LongRunningProcess()
{
// ADO.NET code ...
// Wait for operation to complete
IntPtr[] handles = { asyncResult.AsyncWaitHandle.SafeWaitHandle.DangerousGetHandle() };
uint result = CoWaitForMultipleHandles(0x0001, 30000, 1, handles, out uint index);
if (result == 0x00000080) // RPC_E_CALL_COMPLETE
{
// Operation completed
}
else
{
// Timeout or error
}
}
[DllImport("ole32.dll")]
public static extern uint CoWaitForMultipleHandles(uint dwFlags, int dwTimeout, uint cHandles, IntPtr[] pHandles, out uint pdwIndex);