title: Look Up for new process started using c#
body: <p>I am using the below code to look up for any new process started. This function is running in the thread.</p>
  
  <p>I need to log in the process name started. For which I use two arraylist. On one arraylist
  i store all the process names before it starts the thread and the other arraylist, I fill
  the current process inside the thread and compare two arraylist to find the new process.</p>
  
  <p>The problem now is, since the string to write to the log file is in for each loop, I see
  repeated process names in the log file. I want it to be logged only once. How can I solve this?</p>
  
  <pre><code>class ProcessMonitor
  {
      public static ArrayList ExistingProcess = new ArrayList(); 
  
  
      public static void Monitor()
      { 
          existingProcesses = GetExistingProcess();
  
          while (true)
          {  
              ArrayList currentProcesses = new ArrayList();
              currentProcesses = GetCurrentProcess();
  
              ArrayList NewApps = new ArrayList(GetCurrentProcess());
  
              foreach (var p in ExistingProcess)
              {
                  NewApps.Remove(p); 
              }
              string str = "";
              foreach (string NewApp in NewApps)
              {
                  str += "Launched ProcessName/ID : " + NewApp + "/" + System.Diagnostics.Process.GetProcessesByName(NewApp)[0].Id.ToString() + Environment.NewLine;
              }
               if(str!="")
              {
                 Log.Info(str);
              }
          }
      }
  
      public static ArrayList GetExistingProcess()
      {
          Process[] processlist = Process.GetProcesses();
          foreach (Process Proc in processlist)
          {
              ExistingProcess.Add(Proc.ProcessName);
          }
          return ExistingProcess;
      }
      public static ArrayList GetCurrentProcess()
      {
          ArrayList CurrentProcesses = new ArrayList();
          Process[] processlist =  Process.GetProcesses();
          foreach (Process Proc in processlist)
          {
              CurrentProcesses.Add(Proc.ProcessName);
          }
          return CurrentProcesses;
      }
  }
  </code></pre>