I have code for firefox that shows how much time I spent in it when it is active window
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
private TimeSpan fireFoxElapsedTime = new TimeSpan();
private void timer1_Tick(object sender, EventArgs e)
{
var wnd = GetForegroundWindow();
uint procId;
GetWindowThreadProcessId(wnd, out procId);
var process = Process.GetProcessById((int)procId);
if (process.ProcessName.Equals("firefox", StringComparison.CurrentCultureIgnoreCase))
{
fireFoxElapsedTime += new TimeSpan(0, 0, 0, 0, timer1.Interval);
}
label2.Text = fireFoxElapsedTime.ToString();
}
I need for all the applications whichever is active and data should be stored in sqlite