0

I am currently making an app that tracks users time in windows like togglDisktop app https://toggl.com/ but more advanced with a dashboard, where you can see apps usage automatically without starting a watch, like this

enter image description here

what is note good in this app is that you need always to start a timer which we always forget to do,

I have no idea about how to do it,

I have searched for hours about the class that tracks the user in the windows like the screentime in the iPhone an found nothing, like in this image https://images.app.goo.gl/bGUA9Ei9FtuzzEEf7

I have found something like GetForegroundWindow function but I don't think that it can track the background apps too, its only for the focused app https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow

  • 1
    If you simply want to know what processes are running you can use `Process.GetProcesses` but it will be up to you to determine what is an active "app" and which are system processes. – TheBatman Mar 04 '20 at 21:55
  • this is the crucial part, it has to be automatic to determine what app the user was using and how long the user used the app in the day – bilal BOUASRIA Mar 04 '20 at 21:58
  • Does this answer your question? [get the titles of all open windows](https://stackoverflow.com/questions/7268302/get-the-titles-of-all-open-windows) – TheBatman Mar 04 '20 at 21:59
  • yes, it does in a part of it, I didn't know about the Process.GetProcesses, thank you very much – bilal BOUASRIA Mar 04 '20 at 22:04

2 Answers2

1

You can :

1 - Run a thread in background that keep listening if there is any new process started / closed .

2 - once it gets started for example , you start a new Timer which will end when that process is closed.

3 - You can also get a little bit advanced , check if the Process has an Opened Window Handle , if it has it then check it's state if its minimized = pause the Timer and if its not then resume the timer associated to it .

you can ofcours use winapi for that for example :

IntPtr hwnd  = yourProc.MainWindowHandle ; // this will gets you the process's main window handle .


// to get the window state using winapi :

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);


// WINDOWPLACEMENT structure : 

private struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd; // this one is what you will need to check if minimized / maximized ..etc
    public System.Drawing.Point ptMinPosition;
    public System.Drawing.Point ptMaxPosition;
    public System.Drawing.Rectangle rcNormalPosition;
}


// showCmd  possible cases (to compare the results if GetWindowPlacement returned true ) :

const int SW_HIDE =             0;
const int SW_SHOWNORMAL =       1;
const int SW_NORMAL =           1;
const int SW_SHOWMINIMIZED =    2;
const int SW_SHOWMAXIMIZED =    3;
const int SW_MAXIMIZE =         3;
const int SW_SHOWNOACTIVATE =   4;
const int SW_SHOW =             5;
const int SW_MINIMIZE =         6;
const int SW_SHOWMINNOACTIVE =  7;
const int SW_SHOWNA =           8;
const int SW_RESTORE =          9;

that's just to get you started somewhere hope it helps .

0 x 5 4 4 D
  • 177
  • 1
  • 13
-1

To list all processes running on a machine: Process.GetProcesses

Process object has a lot of properties that you can use like StartTime, ProcessorTime, WorkingSet etc. refer: Process Object

Lastly, to get to know what app/window was active, you will have to possibly use PInvoke. Read: How do I get the title of the current active window using c#?

So your process runs silently and checks what is the current active window and captures the Current timestamp. It also checks/listens for window changes and then book keeps the timings.

Prateek Shrivastava
  • 1,877
  • 1
  • 10
  • 17
  • 4
    (I wasn't the downvoter.) Please read: [Why isn't providing feedback mandatory on downvotes, and why are ideas suggesting such shot down?](https://meta.stackoverflow.com/q/357436/6471538). – Robert Columbia Mar 04 '20 at 22:04