Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Process Monitor

Tags:

c#

process

system

Is there a way to determine when the last time a specific machine last ran a process?

I can use the following to determine if a process is running, but the application cannot grab the process if it has since stopped.

Process[] process = Process.GetProcessesByName(processName, serverName); 
like image 286
Nick Vaccaro Avatar asked Dec 31 '09 17:12

Nick Vaccaro


People also ask

What is the difference between Process Explorer and Process Monitor?

Using it you can find out what files, DLLs, and registry keys particular processes have open and the CPU and memory usage of each. In daily use I often start with Process Explorer to find processes which are consuming a lot of system resources and then move to process monitor to dig deeper into these processes.

How do I run Sysinternals Process Monitor?

Sysinternals Live To do this, open up File Explorer and paste in \\live.sysinternals.com\tools. You'll then see a folder like any ol' network share containing all of the Sysinternals files including procmon. Scroll down until you find procmon, double-click and voila, you're running procmon!


1 Answers

WMI provides a way to track processes starting and terminating with the Win32_ProcessTrace classes. Best shown with an example. Start a new Console application, Project + Add Reference, select System.Management. Paste this code:

using System; using System.Management;  class Process {   public static void Main() {     ManagementEventWatcher startWatch = new ManagementEventWatcher(       new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));     startWatch.EventArrived += new EventArrivedEventHandler(startWatch_EventArrived);     startWatch.Start();     ManagementEventWatcher stopWatch = new ManagementEventWatcher(       new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));     stopWatch.EventArrived += new EventArrivedEventHandler(stopWatch_EventArrived);     stopWatch.Start();     Console.WriteLine("Press any key to exit");     while (!Console.KeyAvailable) System.Threading.Thread.Sleep(50);     startWatch.Stop();     stopWatch.Stop();   }    static void stopWatch_EventArrived(object sender, EventArrivedEventArgs e) {     Console.WriteLine("Process stopped: {0}", e.NewEvent.Properties["ProcessName"].Value);   }    static void startWatch_EventArrived(object sender, EventArrivedEventArgs e) {     Console.WriteLine("Process started: {0}", e.NewEvent.Properties["ProcessName"].Value);   } } 

Edit the manifest so this program runs elevated. Then simply start some programs to see it at work. Beware that it is not especially quick.

like image 164
Hans Passant Avatar answered Sep 28 '22 05:09

Hans Passant