66 lines
2.2 KiB
C#
66 lines
2.2 KiB
C#
// File: Services/Scanners/ThreadScanner.cs
|
|
|
|
using System.Diagnostics;
|
|
using WebmrAPI.Models;
|
|
using WebmrAPI.Utils;
|
|
|
|
namespace WebmrAPI.Services.Scanners
|
|
{
|
|
public class ThreadScanner : AbstractCpuScanner<ProcessThreadInfo>
|
|
{
|
|
override public ScanTarget Target { get => ScanTarget.Threads; }
|
|
internal int PID { get; private set; }
|
|
|
|
override internal bool Scan(out Dictionary<long, ProcessThreadInfo>? data)
|
|
{
|
|
data = new();
|
|
|
|
try
|
|
{
|
|
using (var process = Process.GetProcessById(PID))
|
|
foreach (ProcessThread thread in process.Threads)
|
|
{
|
|
ProcessThreadInfo info;
|
|
|
|
if (GetFromCacheOrNew(thread.Id, out info))
|
|
{
|
|
info.CpuUsage = CalcCpuUsage(info.ProcessorTime, Container.Elapsed.TotalMilliseconds);
|
|
}
|
|
else
|
|
{
|
|
info.ID = thread.Id;
|
|
}
|
|
|
|
info.BasePriority = thread.BasePriority;
|
|
info.CurrentPriority = thread.CurrentPriority;
|
|
info.TotalProcessorTime = thread.TotalProcessorTime;
|
|
|
|
data.Add(info.ID, info);
|
|
}
|
|
}
|
|
catch (System.ComponentModel.Win32Exception ex) when (ex.NativeErrorCode == 5)
|
|
{
|
|
Provider.Logger.LogWarning($"Access denied to process {PID} for thread scanning. Error: {ex.Message}");
|
|
return false;
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
Provider.Logger.LogWarning($"Process {PID} might have exited during thread enumeration. Error: {ex.Message}");
|
|
return false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Provider.Logger.LogError($"An unexpected error occurred while scanning threads for PID {PID}. Error: {ex.Message}");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public ThreadScanner(IScanProvider scanner, LazyConcurrentContainer<ProcessThreadInfo> container, int pid)
|
|
: base(scanner, container)
|
|
{
|
|
PID = pid;
|
|
}
|
|
}
|
|
}
|