using System;
|
using System.Collections.Generic;
|
using System.Diagnostics;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace HH.WCS.QingXigongchang.util
|
{
|
internal class Monnn
|
{
|
private static readonly Dictionary<int, ThreadCpuUsage> _threadUsages = new Dictionary<int, ThreadCpuUsage>();
|
private static readonly Process _currentProcess = Process.GetCurrentProcess();
|
private static TimeSpan _previousTotalProcessorTime;
|
private static DateTime _lastUpdateTime;
|
|
public static void StartMonitoring()
|
{
|
_previousTotalProcessorTime = _currentProcess.TotalProcessorTime;
|
_lastUpdateTime = DateTime.UtcNow;
|
|
UpdateThreadCpuUsage();
|
PrintThreadCpuUsage();
|
//var timer = new Timer(_ =>
|
//{
|
// UpdateThreadCpuUsage();
|
// PrintThreadCpuUsage();
|
//}, null, 0, samplingIntervalMs);
|
}
|
|
private static void UpdateThreadCpuUsage()
|
{
|
var currentTime = DateTime.UtcNow;
|
var timeElapsed = (currentTime - _lastUpdateTime).TotalSeconds;
|
_lastUpdateTime = currentTime;
|
|
var currentTotalProcessorTime = _currentProcess.TotalProcessorTime;
|
var totalCpuUsed = (currentTotalProcessorTime - _previousTotalProcessorTime).TotalMilliseconds;
|
_previousTotalProcessorTime = currentTotalProcessorTime;
|
|
// 计算总可用CPU时间(考虑多核)
|
var totalAvailableCpuTime = timeElapsed * 1000 * Environment.ProcessorCount;
|
|
foreach (ProcessThread thread in _currentProcess.Threads)
|
{
|
try
|
{
|
if (!_threadUsages.TryGetValue(thread.Id, out var usage))
|
{
|
usage = new ThreadCpuUsage(thread.Id);
|
_threadUsages[thread.Id] = usage;
|
}
|
|
var currentThreadTime = thread.TotalProcessorTime;
|
var threadCpuUsed = (currentThreadTime - usage.PreviousProcessorTime).TotalMilliseconds;
|
usage.PreviousProcessorTime = currentThreadTime;
|
|
// 计算CPU使用率百分比
|
usage.CpuUsagePercentage = (threadCpuUsed / totalAvailableCpuTime) * 100;
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"无法监控线程 {thread.Id}: {ex.Message}");
|
}
|
}
|
}
|
|
private static void PrintThreadCpuUsage()
|
{
|
Console.Clear();
|
Console.WriteLine($"进程ID: {_currentProcess.Id} 名称: {_currentProcess.ProcessName}");
|
Console.WriteLine($"总CPU使用率: {_currentProcess.TotalProcessorTime.TotalMilliseconds}ms");
|
Console.WriteLine("线程CPU使用情况:");
|
Console.WriteLine("--------------------------------------------------");
|
Console.WriteLine("ThreadID\tCPU Usage %\tThread State");
|
|
foreach (var usage in _threadUsages.Values.OrderByDescending(u => u.CpuUsagePercentage))
|
{
|
Console.WriteLine($"{usage.ThreadId}\t\t{usage.CpuUsagePercentage:F2}%\t\t{GetThreadState(usage.ThreadId)}");
|
}
|
}
|
|
private static string GetThreadState(int threadId)
|
{
|
try
|
{
|
return _currentProcess.Threads.Cast<ProcessThread>()
|
.FirstOrDefault(t => t.Id == threadId)?.ThreadState.ToString() ?? "Unknown";
|
}
|
catch
|
{
|
return "Unknown";
|
}
|
}
|
|
private class ThreadCpuUsage
|
{
|
public int ThreadId { get; }
|
public TimeSpan PreviousProcessorTime { get; set; }
|
public double CpuUsagePercentage { get; set; }
|
|
public ThreadCpuUsage(int threadId)
|
{
|
ThreadId = threadId;
|
}
|
}
|
}
|
}
|