WCT GetThreadWaitChain函数调用为异步导致进程挂起

时间:2015-12-27 17:04:39

标签: c# interop pinvoke

我正在尝试从我的c#代码中调用Advapi32 GetThreadWaitChain(WCT)函数,我之前在类似问题上遇到过困难: WCT GetThreadWaitChain call allways return false

但是现在我尝试将此调用作为带回调的异步调用,如下所述:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681421(v=vs.85).aspx

这是我的结论:

internal void CollectWaitAsyncInformation(ClrThread thread)
{   
    //Currenlty not working
    var handle = Advapi32.OpenThreadWaitChainSession(WCT_SESSION_OPEN_FLAGS.WCT_ASYNC_OPEN_FLAG, AppCallback);

    uint threadID = thread.OSThreadId;

    WAITCHAIN_NODE_INFO[] NodeInfoArray = new WAITCHAIN_NODE_INFO[WctApiConst.WCT_MAX_NODE_COUNT];

    int isCycle = 0;
    int Count = WctApiConst.WCT_MAX_NODE_COUNT;

    _eventHandler = Kernel32.CreateEvent(IntPtr.Zero, true, true, "MyEvent");

    //This is where the applciation hangs
    bool waitChainResult = Advapi32.GetThreadWaitChain(handle,
                            _eventHandler, 0 ,
                            threadID, ref Count, NodeInfoArray, out isCycle);

    CheckCount(ref Count);

    if (!waitChainResult)
    {

        var lastErrorCode = WinApi.Advapi32.GetLastError();

        if (lastErrorCode == (uint)SYSTEM_ERROR_CODES.ERROR_IO_PENDING)
        {
            // Wait for callback to run...
            WinApi.Kernel32.WaitForSingleObject(_eventHandler, int.MaxValue);
        }
        Kernel32.WaitForSingleObject(_eventHandler, uint.MaxValue);
    }
}

链接:

https://github.com/Pavel-Durov/Multithreading-Debugging-Asignments/blob/master/Assignments/Assignments.Core/Handlers/WCT/WctApiHandler.cs

我的进程挂起,因为我正在调用Advapi32.GetThreadWaitChain函数而没有任何错误等。

上次问题出现在WAITCHAIN_NODE_INFO结构布局中,但是我在这里使用相同的结构,所以我不知道这是不是问题,但如果我将IntPtr.Zero传递给{ {1}}数组我得到的结果是false,但是NodeInfo会返回0。

任何帮助将不胜感激:)。

以下是我整个GitHub项目的链接: https://github.com/Pavel-Durov/Multithreading-Debugging-Asignments

0 个答案:

没有答案