Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

utilizing GetProcesses in .NET to log start and end times for currenlty running processes on yoru machine

Here is a quick C# script to log the start and end times of running processes on your machine.

The idea is query the currently running processes every x number of seconds utilizing the Process.GetProcesses function in .NET, while maintaining a book for currently running processes each cycle, then if a new process appears (or disappears), you log the result as start and end respectively.

Here is how to get currently running processes on your machine using C#:

Process[] currentlyRunning = Process.GetProcesses();

Now run the above line in an endless while loop, and keep track of the process names in an ArrayList, while updating it each cycle.

Here is the full source:

using System;

using System.Collections.Generic;

using System.Text;

using System.Diagnostics;

using System.Threading;

using System.Collections;

namespace ProcessLogger

{

class Program

{

static void Main(string[] args)

{

int iCycleInterval = 1000; // 1 second

ArrayList processesBook = new ArrayList();

bool bFirstCycle = true;

while (true)

{

Process[] currentlyRunning = Process.GetProcesses();

ArrayList currentlyRunningProcessNames = new ArrayList();

// Record starting time for new processes

foreach (Process process in currentlyRunning)

{

if (!processesBook.Contains(process.ProcessName)) // A new process started

{

// Log the Now as the start time for this process (Don't log if this is the the first cycle)

if (!bFirstCycle)

Console.WriteLine(DateTime.Now.ToString() + " Process Started: " + process.ProcessName);

// Book-keep the new process

processesBook.Add(process.ProcessName);

}

// Record the currently running process name for the next for comparison with our book (see he foreach loop below)

currentlyRunningProcessNames.Add(process.ProcessName);

}

// We looped already once, so turn this flag off

bFirstCycle = false;

// Record ending time for finished processes

foreach (string bookKeptProcess in processesBook)

if (!currentlyRunningProcessNames.Contains(bookKeptProcess))

{

// Log the Now as the end time for this process

Console.WriteLine(DateTime.Now.ToString() + " Process Ended: " + bookKeptProcess);

}

// Now Update the process book

processesBook = currentlyRunningProcessNames;

// Sleep till the next loop

Thread.Sleep(iCycleInterval);

}

}

}

}

Tha accuracy of your program will depend on the how frequent you run your loop, i.e. the Cycle Refresh Rate. High frequency would give better accuracy for your log, but be aware that the query fo all running processes is an expensive call (performance-wise), so you want to avoid putting it in a tight loop, otherwise your app will eat up all the CPU time.

Download the full source here:

Source Code

Happy Programming!

kick it on DotNetKicks.com

Feedback

Posted on 3/2/2012 1:39:27 PM

In my humble oppinion using the non-generic ArrayList is a bad idea. List_Process_ is a lot more suited, since you dont have to search for strings and the liststructure is overall better suited, since all we do is iterating the collection.

But nevertheless thanks for sharing your code :)

(had to use underscores instead of angled braces since the sanitizer is to afraight of angles ;) )

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author