I'm not quite sure what you are doing here, but the first two lines and the last line from the excerpt below basically do the same, only the last line is more expensive (since you are creating a second list of arrays from the one that GetCurrentProcess returns:
ArrayList currentProcesses = new ArrayList(); currentProcesses = GetCurrentProcess(); ArrayList NewApps = new ArrayList(GetCurrentProcess());
Secondly, you never seem to use the currentProcess variable, as far as I can tell ... so its 100% waste. Third, why is this a problem if there are duplicate process names? The same process can be started several times, more than one instance of the process can be executed simultaneously, the process can start, stop, then start again, etc. Not necessarily "false" for the process name listed twice.
(UPDATE: One of the reasons you can get "duplicates" in your journal is that you get existingProcesses only once. Each time through a loop (which, by the way, will run at maximum speed continuously), you will getting the list of processes again and comparing them with the original existingProcesses , so the same processes listed in the previous loop ... if they are still running, will be listed again. I updated my sample code to demonstrate how to solve this problem.)
You seem to have some fundamental code errors and possibly flaws in your expectations. I would review your code as a whole, eliminate useless code (for example, the first two lines above) and generally streamline your code. (Hint: ArrayList is REALLY a bad choice ... I would use IEnumerable<T> , which does not require any conversion or a combination of the raw array). If I had to duplicate the code above with more efficient code:
public static void Monitor() { var existingProcesses = Process.GetProcesses(); bool doProcessing = true; while (doProcessing) { var currentProcesses = Process.GetProcesses(); var newProcesses = currentProcesses.Except(existingProcesses); int capacity = newProcesses.Count() * 60; var builder = new StringBuilder(capacity); foreach (var newProcess in newProcesses) { builder.Append("Launched ProcessName/ID : "); builder.Append(newProcess.ProcessName); builder.Append("/"); builder.Append(newProcess.Id); builder.AppendLine(); } string newProcessLogEntry = builder.ToString(); if(!String.IsNullOrEmpty(newProcessLogEntry)) { Log.Info(newProcessLogEntry); } existingProcesses = currentProcesses;
source share