Merge pull request #5 from SlothDpal/cpuCounter

фикс исключения при попытке получить данные внезапно завершенного про…
This commit is contained in:
Vladimir
2024-05-30 14:06:49 +04:00
committed by GitHub
2 changed files with 19 additions and 5 deletions

View File

@@ -241,7 +241,7 @@ namespace Process_Auto_Relaunch
private void Form1_FormClosing(object sender, FormClosingEventArgs e) private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ {
Settings.Default.Save(); Settings.Default.Save();
Status("Наюлюдение отменено - приложение закрыто.", NotifyLevel.logAlways); Status("Наблюдение отменено - приложение закрыто.", NotifyLevel.logAlways);
} }
private bool ProcessByNameIsRuning(string name) private bool ProcessByNameIsRuning(string name)
@@ -286,13 +286,26 @@ namespace Process_Auto_Relaunch
{ {
if (ProcessByNameIsRuning(textBoxProcessName.Text)) if (ProcessByNameIsRuning(textBoxProcessName.Text))
{ {
double cpuTotalTime, cpuPercent;
string ProcessAnswer;
cpuMeasureTimer.Stop(); cpuMeasureTimer.Stop();
double cpuTotalTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds - cpuLastTime; try
cpuLastTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds; {
double cpuPercent = cpuTotalTime * 100 / (Environment.ProcessorCount * cpuMeasureTimer.ElapsedMilliseconds); cpuTotalTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds - cpuLastTime;
string ProcessAnswer = (WatchedProcess.Responding)?"Активен":"Неактивен"; cpuLastTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds;
cpuPercent = cpuTotalTime * 100 / (Environment.ProcessorCount * cpuMeasureTimer.ElapsedMilliseconds);
ProcessAnswer = (WatchedProcess.Responding) ? "Активен" : "Неактивен";
}
catch
{
cpuTotalTime = 0;
cpuPercent = 0;
ProcessAnswer = "Неактивен";
}
cpuMeasureTimer.Reset(); cpuMeasureTimer.Reset();
cpuMeasureTimer.Start(); cpuMeasureTimer.Start();
Status($"Процесс уже запущен.",NotifyLevel.logUpdateStatus); Status($"Процесс уже запущен.",NotifyLevel.logUpdateStatus);
processInformationLabel.Text = $"Интерфейс: {ProcessAnswer}. ЦПУ: {cpuPercent:f2}% {cpuTotalTime:f2}мсек"; processInformationLabel.Text = $"Интерфейс: {ProcessAnswer}. ЦПУ: {cpuPercent:f2}% {cpuTotalTime:f2}мсек";
if (i < (int)numericUpDown1.Value) SendDiscordMessage($"Процесс {textBoxProcessName.Text} запущен.",NotifyLevel.logDiscord); if (i < (int)numericUpDown1.Value) SendDiscordMessage($"Процесс {textBoxProcessName.Text} запущен.",NotifyLevel.logDiscord);

View File

@@ -23,6 +23,7 @@ Global
{B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.DebugFULL|x86.ActiveCfg = Debug|x86 {B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.DebugFULL|x86.ActiveCfg = Debug|x86
{B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.DebugFULL|x86.Build.0 = Debug|x86 {B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.DebugFULL|x86.Build.0 = Debug|x86
{B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x64.ActiveCfg = Release|x64 {B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x64.ActiveCfg = Release|x64
{B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x64.Build.0 = Release|x64
{B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x86.ActiveCfg = Release|x86 {B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x86.ActiveCfg = Release|x86
{B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x86.Build.0 = Release|x86 {B48F106C-F4E2-4BFB-9BAA-42FC13C03FDD}.Release|x86.Build.0 = Release|x86
EndGlobalSection EndGlobalSection