WCT GetThreadWaitChain调用allways返回false

时间:2015-12-07 17:15:57

标签: c# multithreading pinvoke

我正在尝试获取有关某些线程的WCT信息,但每次调用GetThreadWaitChain函数时,我都会得到错误的响应,并且ref参数保持为零。

我做错了什么?

我在调用WaitForMultipleObjects的线程上调用此函数,我确保在调试时此线程等待。

那是我的代码:

    internal void CollectWaitInformation(ClrThread thread)
    {
        var g_WctHandle = OpenThreadWaitChainSession(0, 0);

        uint threadID = thread.OSThreadId;

        WAITCHAIN_NODE_INFO[] NodeInfoArray = new WAITCHAIN_NODE_INFO[WCT_MAX_NODE_COUNT];


        int isCycle = 0;
        int count = 0;

        // Make a synchronous WCT call to retrieve the wait chain.
        bool result = GetThreadWaitChain(g_WctHandle,
                                IntPtr.Zero,
                                WCTP_GETINFO_ALL_FLAGS,
                                threadID, ref count, NodeInfoArray, out isCycle);

        if (!result)
        {
            //error
        }

        //Finaly ...
        CloseSession(g_WctHandle);
    }

    [DllImport("Advapi32.dll")]
    public static extern IntPtr OpenThreadWaitChainSession(OpenThreadChainFlags Flags, DWORD callback);

    [DllImport("Advapi32.dll")]
    public static extern bool GetThreadWaitChain(
        IntPtr WctHandle,
        IntPtr Context,
        UInt32 Flags,
        uint ThreadId,
        ref int NodeCount,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)]
        [In, Out]
        WAITCHAIN_NODE_INFO[] NodeInfoArray,
        out int IsCycle
    );

    [StructLayout(LayoutKind.Sequential)]
    public struct WAITCHAIN_NODE_INFO
    {
        public WCT_OBJECT_TYPE ObjectType;
        public WCT_OBJECT_STATUS ObjectStatus;

        public struct LockObject
        {
            string ObjectName;
            LARGE_INTEGER Timeout;
            BOOL Alertable;
        }
        public struct ThreadObject
        {
            DWORD ProcessId;
            DWORD ThreadId;
            DWORD WaitTime;
            DWORD ContextSwitches;
        }
    }
}

}

在上一个问题的帮助下:

Calling C++ method from C# with pointer parameter (WCT)

1 个答案:

答案 0 :(得分:2)

来自文档:

  

NodeCount [in,out]

     

输入时,从1到WCT_MAX_NODE_COUNT的数字,指定等待链中的节点数。返回时,检索的节点数。

     

...

您未能满足此要求,因为您传入了0.