Click here to Skip to main content
15,887,453 members
Articles / Programming Languages / C#
Tip/Trick

Tracking specific processes running on your computer

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
20 Aug 2010CPOL 8.3K   1  
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;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer CGI
Canada Canada
I have a three years technical education in computer science (June 2006) and a four years bachelor's degree in Software Engineering from the École de Technologie Supérieure (August 2010). I worked as a Web Developper at the beginning of my career before being recruited as a .NET Analyst-Programer at SNC-Lavalin in Montreal. I worked there until they outsourced all development to CGI. I worked there since.

Comments and Discussions

 
-- There are no messages in this forum --