-2

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?

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Tim
  • 11
  • 2
  • Just how many times per second are you calling `Process.GetProcesses()`? –  Feb 25 '17 at 07:31
  • @MickyD This is configurable, usually once a second. But I need to run multiple(now 3) threads at the same time, doing different operations. – Tim Feb 25 '17 at 07:46
  • Perhaps post some of your code? –  Feb 25 '17 at 07:55
  • What is this code supposed to do? – Visual Vincent Feb 25 '17 at 08:49
  • @VisualVincent Protecting a process list, if the protected process has exited, then restart the protected process. – Tim Feb 25 '17 at 09:54
  • Perhaps utilizing a [**WMI alternative**](http://stackoverflow.com/a/857946/3740093) would be a better way of doing this? – Visual Vincent Feb 25 '17 at 09:58
  • @VisualVincent Thanks for your advice! However, using WMI to read ProcessInformationList is not always able to read ProcessFileName. – Tim Feb 25 '17 at 11:51
  • Read the process name yourself via .NET's `Process` class. Just get the ID from WMI and pass it to `Process.GetProcessById()`. – Visual Vincent Feb 25 '17 at 17:16
  • @VisualVincent I tried it, and I found that using the Process.GetProcessById than the Process.GetProcesses processing time longer. – Tim Feb 26 '17 at 03:36

1 Answers1

0

I found another solution in the MSDN forum.

Maybe obtain the Process object, then use WaitForExit. Or set the EnableRaisingEvents to true and handle the Exited event. Then you do not need the periodic loops.

Tim
  • 11
  • 2