Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

c# - WCT GetThreadWaitChain call allways return false

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...