任务管理器

内存

string processName = "sublime_text.exe";
HiPerfTimer.Execute(() =>
{
    Console.WriteLine(WSCommFunc.GetProcessSnapshotInfo(processName));
}, "cost");

using System.Management;
/// <summary>
/// 获取进程快照信息
/// </summary>
/// <param name="processName">进程名称</param>
/// <returns></returns>
public static void GetProcessSnapshotInfo(string processName)
{
    LongThread(() =>
    {
        string result;
        string query = $"SELECT * FROM Win32_Process WHERE Name = '{processName}'";
        var searcher = new ManagementObjectSearcher(query);
        var mo = searcher.Get().OfType<ManagementObject>().FirstOrDefault(); // 获取进程对象
        if (mo != null)
        {
            var threadCnt = mo["ThreadCount"].ToString();
            var procId = mo["ProcessID"].ToString().Value<int>();
            var size = Win32ApiHelper.CalcWorkingSetPrivate(procId);
            result = string.Format("Memory:{0}, ThreadCount:{1}", size, threadCnt);
        }
        else
        {
            result = string.Format("Process [{0}] not found.", processName);
        }
        Console.WriteLine(result);
    });
}
posted @ 2025-04-02 14:25  wesson2019  阅读(8)  评论(0)    收藏  举报