фикс исключения при попытке получить данные внезапно завершенного процесса.

This commit is contained in:
slothdpal
2024-05-29 22:28:35 +03:00
parent 7ab58bd6e8
commit dd56aaab9e

View File

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