Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get WCT information about some thread, but every time I am calling GetThreadWaitChain function I am getting false as a response and the ref parameters remain zero.

What am I doing wrong?

I am calling this function on a thread which calls WaitForMultipleObjects, and I am making sure that this thread waits while I am debugging.

That's my code:

    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;
        }
    }
}

}

With a help from my previous question:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
389 views
Welcome To Ask or Share your Answers For Others

1 Answer

From the documentation:

NodeCount [in, out]

On input, a number from 1 to WCT_MAX_NODE_COUNT that specifies the number of nodes in the wait chain. On return, the number of nodes retrieved.

....

You fail to meet this requirement because you pass in 0.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...