Thread Name in WPA

Razvan Sirbu 0 Reputation points
2023-08-24T18:49:31.1333333+00:00

Hello,

I'm trying to add the Thread Name to my source code (VS2015, C#, WinForms app) to be displayed in WPA.

Using the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace AAA
{
    static class Program
    {
        [DllImport("Kernel32", EntryPoint = "GetCurrentThread", ExactSpelling = true)]
        public static extern IntPtr GetCurrentWin32Thread();
 
        [DllImport("Kernel32", EntryPoint = "SetThreadDescription", ExactSpelling = true, CharSet = CharSet.Unicode)]
        public static extern long SetThreadDescription(IntPtr thHandle, [MarshalAs(UnmanagedType.LPWStr)] string name);
 
        static void SetNativeThName(string sThName)
        {
            try
            {
                IntPtr thH = GetCurrentWin32Thread();
                long res = SetThreadDescription(thH, sThName);
            }
            catch (Exception ex)
            {
                var hr = ex.HResult;
            }
        }
 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            SetNativeThName("MyMainThUI");
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MyFinMain();
            Application.Run(new Form1());
        }
        static public void MyFinMain()
        {
            Thread.Sleep(10);
        }
    }
}

I get only the Thread names in the SystemActivity.Threads Lifetimes but not under the CPU precise - Utilization by Process, Thread, Stack

User's image

Thanks,

Razvan

Windows Performance Toolkit
Windows Performance Toolkit
A collection of Microsoft performance monitoring tools that produce in-depth performance profiles of Windows operating systems and applications.
107 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.