add cpuCounter

This commit is contained in:
SlothDpal
2024-05-27 11:22:16 +03:00
parent 6349357d7a
commit fe95025e27

View File

@@ -31,6 +31,9 @@ namespace Process_Auto_Relaunch
private readonly UpdateLogDelegate updateLogDelegate; private readonly UpdateLogDelegate updateLogDelegate;
private DiscordWebhook dwhHook; private DiscordWebhook dwhHook;
private DiscordMessage dwhMessage; private DiscordMessage dwhMessage;
private Process WatchedProcess;
private double cpuLastTime = 0;
private Stopwatch cpuMeasureTimer;
public Form1() public Form1()
{ {
@@ -40,17 +43,7 @@ namespace Process_Auto_Relaunch
this.updateLogDelegate += this.HistoryLog; this.updateLogDelegate += this.HistoryLog;
myBackgroundWorker.WorkerSupportsCancellation = true; myBackgroundWorker.WorkerSupportsCancellation = true;
dwhHook = new DiscordWebhook(); dwhHook = new DiscordWebhook();
/*if ( Uri.IsWellFormedUriString(Settings.Default.dwhURL,UriKind.Absolute) && Settings.Default.dwhEnabled && Settings.Default.dwhURL!="") cpuMeasureTimer = new Stopwatch();
{
dwhHook.Url = Settings.Default.dwhURL;
}
else if (Settings.Default.dwhEnabled) {
Debug.WriteLine($"Îøèáêà â URL âåá-õóêà ({Settings.Default.dwhURL}). Âûâîä â Discord îòêëþ÷åí.");
HistoryLog($"Îøèáêà â URL âåá-õóêà ({Settings.Default.dwhURL}). Âûâîä â Discord îòêëþ÷åí.");
Settings.Default.dwhEnabled = false;
Settings.Default.Save();
}*/
} }
/// <summary> /// <summary>
@@ -248,6 +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);
} }
private bool ProcessByNameIsRuning(string name) private bool ProcessByNameIsRuning(string name)
@@ -258,9 +252,11 @@ namespace Process_Auto_Relaunch
{ {
Debug.WriteLine($"Found proces: {process.ProcessName}. Session Id: {process.SessionId}. Current Session Id: {sessionid}"); Debug.WriteLine($"Found proces: {process.ProcessName}. Session Id: {process.SessionId}. Current Session Id: {sessionid}");
if (process.SessionId == sessionid) if (process.SessionId == sessionid)
{
WatchedProcess = process;
return true; return true;
}
} }
Debug.WriteLine($"Process {name} for current session id {sessionid} not found"); Debug.WriteLine($"Process {name} for current session id {sessionid} not found");
return false; return false;
} }
@@ -276,7 +272,8 @@ namespace Process_Auto_Relaunch
} }
Status("Ïðîöåññ áûë çàïóùåí.", NotifyLevel.logAlways); Status("Ïðîöåññ áûë çàïóùåí.", NotifyLevel.logAlways);
Process.Start(path, args); WatchedProcess=Process.Start(path, args);
cpuMeasureTimer.Start();
} }
private void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e) private void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
@@ -288,7 +285,13 @@ namespace Process_Auto_Relaunch
{ {
if (ProcessByNameIsRuning(textBoxProcessName.Text)) if (ProcessByNameIsRuning(textBoxProcessName.Text))
{ {
Status($"Ïðîöåññ óæå çàïóùåí",NotifyLevel.logUpdateStatus); cpuMeasureTimer.Stop();
double cpuTotalTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds - cpuLastTime;
cpuLastTime = WatchedProcess.TotalProcessorTime.TotalMilliseconds;
double cpuPercent = cpuTotalTime * 100 / (Environment.ProcessorCount * cpuMeasureTimer.ElapsedMilliseconds);
cpuMeasureTimer.Reset();
cpuMeasureTimer.Start();
Status($"Ïðîöåññ óæå çàïóùåí. CPU: {cpuPercent:f2}% {cpuTotalTime:f2}ìñåê",NotifyLevel.logUpdateStatus);
if (i < (int)numericUpDown1.Value) SendDiscordMessage($"Ïðîöåññ {textBoxProcessName.Text} çàïóùåí.",NotifyLevel.logDiscord); if (i < (int)numericUpDown1.Value) SendDiscordMessage($"Ïðîöåññ {textBoxProcessName.Text} çàïóùåí.",NotifyLevel.logDiscord);
i = (int)numericUpDown1.Value; i = (int)numericUpDown1.Value;
} }