My application at the moment uses the dll user32 to track how long a user is inactive. My problem is that I do not want to have to write a million queries to find how long a user spends on firefox or word or which ever application is running. What I want is some way to use a timer to calculate how long a process is actively used for. then save this information into a database
Asked
Active
Viewed 369 times
0
-
Start your timer in your form LostFocus event? – Kilazur May 15 '14 at 10:39
-
1How about System.Diagnostics.Stopwatch? you can get elapsed time in `TimeSpan` object type variable. – Shell May 15 '14 at 10:44
-
1The "million queries" angle is very obtuse, use System.Management to query with the Win32_Process class. Which allows both enumerating processes and finding out how much processor time they spent. And of course always keep your user's desire for privacy in mind, nobody *ever* likes to be spied on. – Hans Passant May 15 '14 at 11:24
-
@ Hans Passant: I know it is not millions however the queries I had meant were IN access. As far as privacy is concerned this is will only track the processes that were running not the folder name browser urls etc as that information would be useless for the program – Broken_Code May 15 '14 at 12:01
-
http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.totalprocessortime – Cody Gray - on strike May 15 '14 at 12:06
1 Answers
0
Form_Activate
event is raised whenever the form receives the focus. Likewise, when the form loses focus Form_Deactivate
event is raised. You can use these events to find out when the form get the focus and when it lost it.
DateTime activated;
protected void Form_Activated(Object sender, EventArgs e)
{
activated = DateTime.Now;
}
protected void Form_Deactivated(Object sender, EventArgs e)
{
DateTime deactivated = DateTime.Now;
TimeSpan ts = deactivated - activated; // the duration for which the form was active
// code to store the data activated time/deactivated time/ or duration only in the database
}

Manish Dalal
- 1,768
- 1
- 10
- 14
-
The problem is I want to track all applications not my application – Broken_Code May 15 '14 at 11:59
-
See http://stackoverflow.com/questions/11711400/how-to-monitor-focus-changes and http://stackoverflow.com/questions/2183541/c-detecting-which-application-has-focus – Manish Dalal May 15 '14 at 12:27