A few days back I got the idea of figuring out how much time I was spending on different application while working. So I made this small console application. It's not perfect but it can give you a head start. So I am using the following three classes. There is no complex code but still i will try to explain it.
In this test app I am collecting the data for five applications, but you can always extend it.
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. ActiveApplicationPropertyThread activeWorker = new ActiveApplicationPropertyThread();
  6. Thread activeThread = new Thread(activeWorker.StartThread );
  7. activeThread.Start();
  8. Dictionary<string, Double> temp;
  9. while(true)
  10. {
  11. Console.WriteLine(activeWorker.AppInfo);
  12. if (activeWorker.activeApplicationInfomationCollector._focusedApplication.Keys.Count > 5)
  13. {
  14. temp = activeWorker.activeApplicationInfomationCollector._focusedApplication;
  15. break;
  16. }
  17. Thread.Sleep(1000);
  18. }
  19. foreach (var j in temp.Keys)
  20. {
  21. Console.WriteLine(j + " " + temp[j] / 1000);
  22. }
  23. }
    }
This is the main thread that is monitoring the application and checking if these applications is started and if it is, it will log it and start a time against it.
  1. public class ActiveApplicationPropertyThread
  2. {
  3. private bool _stopThread = true;
  4. private string _appName = "Idle";
  5. [DllImport("user32.dll")]
  6. static extern int GetForegroundWindow();
  7. [DllImport("user32")]
  8. private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
  9. public ActiveApplicationInfomationCollector activeApplicationInfomationCollector;
  10. private string _justSentApplicationName = "";
  11. public ActiveApplicationPropertyThread()
  12. {
  13. activeApplicationInfomationCollector = new ActiveApplicationInfomationCollector();
  14. }
  15. public void StopThread()
  16. {
  17. _stopThread = false;
  18. }
  19. public void StartThread()
  20. {
  21. while(_stopThread)
  22. {
  23. ActiveAppName();
  24. if(_justSentApplicationName == "")
  25. {
  26. _justSentApplicationName = _appName;
  27. activeApplicationInfomationCollector.ApplicationFocusStart(_appName);
  28. }
  29. if(_justSentApplicationName != _appName )
  30. {
  31. activeApplicationInfomationCollector.ApplicationFocusEnd(_justSentApplicationName);
  32. activeApplicationInfomationCollector.ApplicationFocusStart(_appName);
  33. _justSentApplicationName = _appName;
  34. }
  35. Thread.Sleep(2000);
  36. }
  37. }
  38. public string AppInfo
  39. {
  40. get { return _appName; }
  41. }
  42. private Int32 GetWindowProcessID(Int32 hwnd)
  43. {
  44. Int32 pid = 1;
  45. GetWindowThreadProcessId(hwnd, out pid);
  46. return pid;
  47. }
  48. private void ActiveAppName()
  49. {
  50. Int32 hwnd = 0;
  51. hwnd = GetForegroundWindow();
  52. try
  53. {
  54. _appName = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.ModuleName;
  55. }catch(Exception e)
  56. {
  57. }
  58. }
    }
This helper class is just collecting data for different applications and adding the total time being used.
  1. public class ActiveApplicationInfomationCollector
  2. {
  3. public DateTime _startTime;
  4. public Dictionary<string, Double> _focusedApplication;
  5. public ActiveApplicationInfomationCollector()
  6. {
  7. _focusedApplication = new Dictionary<string, Double>();
  8. }
  9. public void ApplicationFocusStart(string appName)
  10. {
  11. _startTime = DateTime.Now;
  12. if(!_focusedApplication.ContainsKey(appName ))
  13. {
  14. _focusedApplication.Add(appName, 0);
  15. }
  16. }
  17. public void ApplicationFocusEnd(string appName)
  18. {
  19. //end the timer and update the seconds
  20. TimeSpan elapsed = DateTime.Now - _startTime;
  21. _focusedApplication[appName] = _focusedApplication[appName] + elapsed.TotalMilliseconds ;
  22. }
  23. }