From c50828633a4fe54515803026248067387ee45a7b Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 6 Jun 2024 22:36:16 +0400 Subject: [PATCH] Detect responding process Responding + cpu --- Form1.cs | 49 ++++++++++++++++++++++++++++++++++++ Process Auto Relaunch.csproj | 2 ++ 2 files changed, 51 insertions(+) diff --git a/Form1.cs b/Form1.cs index 9bfe95c..da052bb 100644 --- a/Form1.cs +++ b/Form1.cs @@ -12,6 +12,7 @@ using Discord.Webhook; using System.Data; using static System.Net.Mime.MediaTypeNames; using RelaunchProcess; +using System.Timers; namespace Process_Auto_Relaunch @@ -34,6 +35,8 @@ namespace Process_Auto_Relaunch private Process WatchedProcess; private double cpuLastTime = 0; private Stopwatch cpuMeasureTimer; + private System.Timers.Timer waitResponceTimer; + /// /// Процесс для наблюдения /// @@ -262,6 +265,51 @@ namespace Process_Auto_Relaunch return false; } + private void ProcessCheckResponding(bool cpuResponding, double cpuPrecent) + { + if (cpuResponding || cpuPrecent > 0.01) + { + if (waitResponceTimer != null) + { + StopTimerWaitingResponce(); + } + return; + } + + if (!TimerResponceRuning()) + { + StartTimerWaitingResponce(); + } + } + + private bool TimerResponceRuning() + { + return waitResponceTimer != null /*&& waitResponceTimer.Enabled*/; + } + + private void StartTimerWaitingResponce() + { + Debug.WriteLine("Запуск таймера ожидания процесса."); + waitResponceTimer = new System.Timers.Timer(5000); + waitResponceTimer.Elapsed += ProcessNotResponding; + waitResponceTimer.AutoReset = false; + waitResponceTimer.Enabled = true; + } + + private void StopTimerWaitingResponce() + { + Debug.WriteLine("Остановка таймера ожидания процесса."); + waitResponceTimer.Dispose(); + waitResponceTimer = null; + } + + private void ProcessNotResponding(Object source, ElapsedEventArgs e) + { + Debug.WriteLine("Таймер ожидания ответа процесса вышел."); + Status($"Процесс {ProcessName} не отвечает. @everyone сделайте что-нибудь!", NotifyLevel.logDiscord); + Status($"Процесс {ProcessName} не отвечает.", NotifyLevel.logHistory); + } + private void ProcessStart(string path, string args) { if (checkBoxCheckProcess.Checked) @@ -297,6 +345,7 @@ namespace Process_Auto_Relaunch cpuLastTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds; cpuPercent = cpuTotalTime * 100 / (Environment.ProcessorCount * cpuMeasureTimer.ElapsedMilliseconds); ProcessAnswer = (WatchedProcess.Responding) ? "Активен" : "Неактивен"; + ProcessCheckResponding(WatchedProcess.Responding, cpuPercent); } catch { diff --git a/Process Auto Relaunch.csproj b/Process Auto Relaunch.csproj index f0cf167..92de599 100644 --- a/Process Auto Relaunch.csproj +++ b/Process Auto Relaunch.csproj @@ -38,6 +38,8 @@ x64 bin\x64\Debug\ + DEBUG + true x64