I develop features require regular scans running processes list, restart the process of exiting the exception.
I use Process.GetProcesses()
to get the list of currently running processes, which on most computers does not have a performance problem.
But when I deployed on a server that is running for a long time (has been running for 147 days, new PID have been million), using the Process.GetProcesses()
processing time up to 100 milliseconds, and normal Process.GetProcesses()
processing time is within a few milliseconds. This affects system performance.
ProcessesProtectionThread = new Thread(() => {
try {
while (true) {
try {
//I use the Stopwatch, and I found that almost always deal with this line
// of code.
var currentProcesses = Process.GetProcesses();
var protectedProcesses = currentProcesses.Where(item => protectProcessesNames.Contains(item.ProcessName.ToUpper()));
var protectedProcessesPaths = protectedProcesses.Select(item => item?.MainModule?.FileName?.ToUpper()).Distinct().ToList();
var restartProcessesPaths = protectProcessesPaths.Where(item => !protectedProcessesPaths.Contains(item)).ToList();
RestartProcess(restartProcessesPaths, currentProcesses);
}
catch (ThreadAbortException) { }
catch (Exception ex) { LogHelper.WriteExMessage(ex); }
finally { Thread.Sleep(ProcessProtectionInterval); }
}
}
catch (ThreadAbortException) { }
catch (Exception ex) { LogHelper.WriteExMessage(ex); }
});
ProcessesProtectionThread.IsBackground = true;
ProcessesProtectionThread.Start();
Who can tell me how to solve this problem?