Detect responding process

Responding + cpu
This commit is contained in:
Vladimir
2024-06-06 22:36:16 +04:00
parent 18403029d0
commit c50828633a
2 changed files with 51 additions and 0 deletions

View File

@@ -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;
/// <summary>
/// Процесс для наблюдения
/// </summary>
@@ -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
{

View File

@@ -38,6 +38,8 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>