0

I'm new to Stack Overflow.

I'd thought I'd start with a puzzling question. How can I monitor the system and detect when new processes are created and when they are closed.

I wouldn't have the slightest idea on how to do this other that a constantly refreshing listbox polling System.Diagnostics.Process.GetProcesses() every second.

I need a monitor that fires an event when a new process is added and a seperate event that fires when a process is closed.

I'm not looking for a process monitor that runs on a loop or a timer. I need a direct interface to the process list, so this thread .NET Process Monitor Does not answer my question. Ideas?

-- Caustic

CausticLasagne
  • 179
  • 1
  • 12
  • Expanding on this, If I had some kind of class that for example when a program is opened or a new process is started it fires NewProcessCreated(processID,processName,processPublisher); – CausticLasagne Jun 27 '16 at 17:27
  • my approach doesn't use timer, it is based on events. this is what you want – magicandre1981 Jun 28 '16 at 15:31

2 Answers2

0

you can use Process.GetProcessesByName Method (String) to find whether a particular process is running or not.

 System.Diagnostics.Process[] proc = System.Diagnostics.Process.GetProcessesByName(sProcessName);
  if (proc.Length > 0)
  {
      MessageBox.Show(String.Format("{0}is  running!", sProcessName), sProcessName);
  }
  else
  {
      MessageBox.Show(String.Format("{0}is not running!", sProcessName), sProcessName);

   }
  • I'm not sure that is quite what OP asked. OP would like to constantly monitor processes rather than just check on occasion if one is there – Oliver Jun 27 '16 at 18:40
  • He can use timer event or stopwatch class event to continuously monitor –  Jun 27 '16 at 18:43
0

I would use TraceEvent, to start a Realtime Session and activate provider Microsoft-Windows-Kernel-Process

enter image description here

In the code you can see the ProcessStart and ProcessStop handling and here you can add your own code to handle when you get this data.

magicandre1981
  • 27,895
  • 5
  • 86
  • 127