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.
- class Program
- {
- static void Main(string[] args)
- {
- ActiveApplicationPropertyThread activeWorker = new ActiveApplicationPropertyThread();
- Thread activeThread = new Thread(activeWorker.StartThread );
- activeThread.Start();
- Dictionary<string, Double> temp;
- while(true)
- {
- Console.WriteLine(activeWorker.AppInfo);
- if (activeWorker.activeApplicationInfomationCollector._focusedApplication.Keys.Count > 5)
- {
- temp = activeWorker.activeApplicationInfomationCollector._focusedApplication;
-
- break;
- }
-
- Thread.Sleep(1000);
- }
-
- foreach (var j in temp.Keys)
- {
- Console.WriteLine(j + " " + temp[j] / 1000);
-
- }
- }
}
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.
- public class ActiveApplicationPropertyThread
- {
- private bool _stopThread = true;
- private string _appName = "Idle";
- [DllImport("user32.dll")]
- static extern int GetForegroundWindow();
- [DllImport("user32")]
- private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
-
- public ActiveApplicationInfomationCollector activeApplicationInfomationCollector;
- private string _justSentApplicationName = "";
- public ActiveApplicationPropertyThread()
- {
- activeApplicationInfomationCollector = new ActiveApplicationInfomationCollector();
- }
-
- public void StopThread()
- {
- _stopThread = false;
- }
-
- public void StartThread()
- {
- while(_stopThread)
- {
- ActiveAppName();
-
- if(_justSentApplicationName == "")
- {
- _justSentApplicationName = _appName;
- activeApplicationInfomationCollector.ApplicationFocusStart(_appName);
- }
- if(_justSentApplicationName != _appName )
- {
- activeApplicationInfomationCollector.ApplicationFocusEnd(_justSentApplicationName);
- activeApplicationInfomationCollector.ApplicationFocusStart(_appName);
- _justSentApplicationName = _appName;
- }
-
- Thread.Sleep(2000);
- }
- }
-
- public string AppInfo
- {
- get { return _appName; }
- }
-
- private Int32 GetWindowProcessID(Int32 hwnd)
- {
- Int32 pid = 1;
- GetWindowThreadProcessId(hwnd, out pid);
- return pid;
- }
-
- private void ActiveAppName()
- {
- Int32 hwnd = 0;
- hwnd = GetForegroundWindow();
- try
- {
- _appName = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.ModuleName;
- }catch(Exception e)
- {
-
- }
-
- }
}
This helper class is just collecting data for different applications and adding the total time being used.
- public class ActiveApplicationInfomationCollector
- {
-
- public DateTime _startTime;
- public Dictionary<string, Double> _focusedApplication;
- public ActiveApplicationInfomationCollector()
- {
- _focusedApplication = new Dictionary<string, Double>();
- }
- public void ApplicationFocusStart(string appName)
- {
- _startTime = DateTime.Now;
- if(!_focusedApplication.ContainsKey(appName ))
- {
- _focusedApplication.Add(appName, 0);
-
- }
- }
- public void ApplicationFocusEnd(string appName)
- {
-
- TimeSpan elapsed = DateTime.Now - _startTime;
- _focusedApplication[appName] = _focusedApplication[appName] + elapsed.TotalMilliseconds ;
- }
-
- }