Track The Time Spend on Different Application

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.                       
  16.                 break;  
  17.             }  
  18.                       
  19.             Thread.Sleep(1000);  
  20.         }  
  21.   
  22.         foreach (var j in temp.Keys)  
  23.         {  
  24.             Console.WriteLine(j + "         " + temp[j] / 1000);  
  25.   
  26.         }  
  27.     }  
    }  
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.   
  10.    public ActiveApplicationInfomationCollector activeApplicationInfomationCollector;  
  11.    private string _justSentApplicationName = "";  
  12.    public ActiveApplicationPropertyThread()  
  13.    {  
  14.        activeApplicationInfomationCollector = new ActiveApplicationInfomationCollector();  
  15.    }  
  16.   
  17.    public void StopThread()  
  18.    {  
  19.        _stopThread = false;  
  20.    }  
  21.   
  22.    public void StartThread()  
  23.    {  
  24.        while(_stopThread)  
  25.        {  
  26.            ActiveAppName();  
  27.   
  28.            if(_justSentApplicationName == "")  
  29.            {  
  30.                _justSentApplicationName = _appName;  
  31.                activeApplicationInfomationCollector.ApplicationFocusStart(_appName);  
  32.            }  
  33.            if(_justSentApplicationName != _appName )  
  34.            {  
  35.                activeApplicationInfomationCollector.ApplicationFocusEnd(_justSentApplicationName);  
  36.                activeApplicationInfomationCollector.ApplicationFocusStart(_appName);  
  37.                _justSentApplicationName = _appName;  
  38.            }  
  39.                  
  40.            Thread.Sleep(2000);  
  41.        }  
  42.    }  
  43.   
  44.    public string AppInfo  
  45.    {  
  46.        get { return _appName; }  
  47.    }  
  48.   
  49.    private Int32 GetWindowProcessID(Int32 hwnd)  
  50.    {  
  51.        Int32 pid = 1;  
  52.        GetWindowThreadProcessId(hwnd, out pid);  
  53.        return pid;  
  54.    }  
  55.   
  56.    private void ActiveAppName()  
  57.    {  
  58.        Int32 hwnd = 0;  
  59.        hwnd = GetForegroundWindow();  
  60.        try  
  61.        {  
  62.            _appName = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.ModuleName;    
  63.        }catch(Exception e)  
  64.        {  
  65.                  
  66.        }  
  67.              
  68.    }  
    }  
This helper class is just collecting data for different applications and adding the total time being used. 
  1. public class ActiveApplicationInfomationCollector  
  2. {  
  3.         
  4.     public DateTime _startTime;  
  5.     public Dictionary<string, Double> _focusedApplication;  
  6.     public ActiveApplicationInfomationCollector()  
  7.     {  
  8.         _focusedApplication = new Dictionary<string, Double>();  
  9.     }  
  10.     public void ApplicationFocusStart(string appName)  
  11.     {  
  12.         _startTime = DateTime.Now;  
  13.         if(!_focusedApplication.ContainsKey(appName ))  
  14.         {  
  15.             _focusedApplication.Add(appName, 0);  
  16.   
  17.         }  
  18.     }  
  19.     public void ApplicationFocusEnd(string appName)  
  20.     {  
  21.         //end the timer and update the seconds  
  22.         TimeSpan elapsed = DateTime.Now - _startTime;  
  23.         _focusedApplication[appName] = _focusedApplication[appName] + elapsed.TotalMilliseconds ;  
  24.     }  
  25.   
  26. }
Ebook Download
View all
Learn
View all