I'm trying to display the active time of different windows apps. like if the user change the window from Notepad to browser then the Notepad Active time will be displayed. This is output right now but the time is not accurate. i got the output using stopwatch. Any suggesion?
public AppInfo GetActiveWindowTitle()
{
var handle = GetForegroundWindow();
string name = "";
uint pid = 0;
GetWindowThreadProcessId(handle, out pid);
Process p = Process.GetProcessById((int)pid);
var processname = p.ProcessName;
AppInfo appInfo = new AppInfo();
appInfo.appname = processname;
name = GetTitle(handle);
appInfo.apptitle = name;
if (!st.Contains(appInfo.apptitle)) {
if (!st.Contains(appInfo.appname))
{
stopWatch.Reset();
st.Push(appInfo.apptitle);
st.Push(appInfo.appname);
///appInfo.startappTime = DateTime.Now;
stopWatch.Start();
}
}
else
{
stopWatch.Stop();
// appInfo.endappTime = DateTime.Now;
// appInfo.finalAppTime = appInfo.endappTime.Subtract(appInfo.startappTime);
finalAppTime = stopWatch.Elapsed;
appInfo.AppTotalTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",finalAppTime.Hours, finalAppTime.Minutes, finalAppTime.Seconds,finalAppTime.Milliseconds / 10);
string appTitle = Convert.ToString(st.Pop());
string appName = Convert.ToString(st.Pop());
richTextBox1.Text = richTextBox1.Text + "\nApp Title= " + appTitle + "-------App Name =" + appName + "-----------------App Time =" + appInfo.AppTotalTime;
}
return appInfo;}
AppInfo appinfo = GetActiveWindowTitle();
richTextBox1.Text = richTextBox1.Text + "\n" + appinfo.appname + "-------" + appinfo.apptitle + "-----------------" + appinfo.AppTotalTime ;