tags:

views:

80

answers:

4

I want to make a program that monitors the other programs running on Windows. It needs to know for instance "Max Payne 2 opened at 4:36pm" or "Firefox closed at 9:52 am." Ideally, it would be able to tell the difference between when I'm actually using the program, or if I have alt-tabbed out of it or if I'm inactive. How do I extract that data? Could someone please just point me in the right direction so I know what phrase I need to google or what topic I need to pick up a book on?

+5  A: 

You can do this in order to list the running processes:

  using System.Diagnostics;

 .......


  Process[] processlist = Process.GetProcesses();

     foreach(Process theprocess in processlist){
     Console.WriteLine(”Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
  }

Furthermore, you may use the following properties to retrieve some interesting info:

 theprocess.StartTime (Shows the time the process started)
 theprocess.TotalProcessorTime (Shows the amount of CPU time the process has taken)
 theprocess.Threads ( gives access to the collection of threads in the process)
Sander Pham
That's C#, right? I googled "System.Diagnostics" to figure out the language and the result seems to show it's C#, but I would appreciate a confirmation. I'm a programming beginner in case it wasn't obvious or you didn't notice it in the tag.
lala
Yes, System.Diagnostics, C#
Sander Pham
A: 

Monitor the services. Log the data in a database or in flat files.monitoriing services with .NET

buckbova
A: 

I would recommend C#. You can start with @Sander Pham's example. Check out this article for more information: http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx

You would have to enumerate local processes every few seconds or so, so you can monitor when which of them close and open. To figure out if they are being used, you can use some Win32 functions to determine if the windows are active or not. Check this out for starters: "GetForegroundWindow Function".

For examples in C#, just Google "c# get window focused".

SimpleCoder
@Simple: he didn't ask for a language recommendation. Also, please don't post old links.
John Saunders
The language recommendation was relevant to the question, so I appreciate it. Also, I'm not a he.
lala
@John SaundersI appreciate your removing the bad links, but because she didn't specify what language she was using, I thought I would recommend one
SimpleCoder
A: 

The Windows API has a helper function called CreateToolhelp32Snapshot.

Marcus Adams
@Marcus: please don't post old links. There's nothing in the question that suggests the OP needs the "VS.85" version of the information.
John Saunders
@John, thanks for updating the link, but please skip the lecture next time. You can blame Google for that one.
Marcus Adams
@Marcus: Actually, I blame you. You apparently know that MSDN includes versions in the links. Next time, look before you post and the problem won't arise again. Besides, I said "please".
John Saunders
@John, I said please too, and I didn't know about versions until now. You assume the worst. Next time, try something like, "Marcus, I updated your link. In case you weren't aware, MSDN has versions in their links."
Marcus Adams
@Marcus: I assumed the usual. Most people know about the versions. BTW, the issue with the old links is that, as you follow links from one old page, you get more old links. Someone who thought he was looking at current information could wind up with old information about something totally unrelated, just by following an old link.
John Saunders