65.9K
CodeProject is changing. Read more.
Home

Tracking specific processes running on your computer

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (2 votes)

Aug 19, 2010

CPOL
viewsIcon

8447

Ever wanted to track for how much time a specific process have been running? Here's an easy way to do so.

In this code, we track for "notepad" and "calc". If you try to track ALL the processes, you'll have problems with process that are "restricted". The error is catched and will throw something in the console. I'd like to point out that I inspired myself from various sources but none worked as I liked. This one is an modified version of the code here: http://www.mycsharpcorner.com/Post.aspx?postID=24[^].
/* Process struct. A process is unique with its name and startTime. We could also take other info if we want to be sure. It's useful when there's multiple instances of a same process running. Right now, it wouldn't work if 2 processes were started exactly at the same time.. in that case.. we wouldn't care which one that is closed first.*/
struct ProcessInfo
{
  public String Name;
  public DateTime StartTime;
}

// List of the processes that we scan for.
ArrayList watchedProcesses = new ArrayList();

// List of the currently running processes that we watch.
ArrayList processesBook = new ArrayList();

// Check for new open process and record ended ones.
private void timerScan_Tick(object sender, EventArgs e)
{
  watchedProcesses.Add("notepad");
  watchedProcesses.Add("calc");
  ArrayList currentlyRunningProcessNames = new ArrayList();
  Process[] currentlyRunning = Process.GetProcesses();

  // Record starting time for new processes
  foreach(Process process in currentlyRunning)
  {
    if(watchedProcesses.Contains(process.ProcessName))
    {
      try
      {
        ProcessInfo currentProcess = new ProcessInfo();
        currentProcess.Name = process.ProcessName;
        currentProcess.StartTime = process.StartTime;

        // A new process started
        if(!processesBook.Contains(currentProcess))
        {
          /* Log the Now as the start time for this process (Don't log if this is the the first cycle)*/
          System.Diagnostics.Debug.WriteLine(currentProcess.StartTime + " | Process Started: " + currentProcess.Name + " | Running: " + new TimeSpan(DateTime.Now.Ticks - currentProcess.StartTime.Ticks).ToString());
        
          // Book-keep the new process
          processesBook.Add(currentProcess);
        }

      // Record the currently running process name. If one of the process in the book is NOT in this list later, this mean it has been closed.
      currentlyRunningProcessNames.Add(currentProcess);
    }
    catch(System.ComponentModel.Win32Exception err)
    {
      System.Diagnostics.Debug.WriteLine(process.ProcessName + " cannot be scanned. " + err.Message);
    }
  }
}

// Record ending time for finished processes
foreach(ProcessInfo bookKeptProcess in processesBook)
{
  /* Check if one of the process of the book is not currently running. If it's not currently running, this mean it has been closed. We don't have to delete anything, we'll copy the ArrayList after the loop.*/
  if(!currentlyRunningProcessNames.Contains(bookKeptProcess))
  {
    // Log the Now as the end time for this process
    System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString() + " | Process Ended: " + bookKeptProcess.Name + " | Running: " + new TimeSpan(DateTime.Now.Ticks - bookKeptProcess.StartTime.Ticks).ToString());
  }
}

  // Now Update the process book
  processesBook = currentlyRunningProcessNames;
}