-1

How can I get the total time this application spent in the foreground, measured in milliseconds? When in the foreground, the user is actively interacting with the application. I need it for all windows installed applications every hour.

The closest solution that I found - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.totalprocessortime?view=net-5.0 But this solution is per process and without consideration process state (background/foreground)

Rougher
  • 834
  • 5
  • 19
  • 46
  • Did you meant application form active time? If so, https://stackoverflow.com/questions/63547309/active-time-of-current-application-using-c-sharp does this answer your question. – zia khan Apr 22 '21 at 14:24
  • I mean total time in foreground for all installed applications. Like in Android: https://developer.android.com/reference/android/app/usage/UsageStats#getTotalTimeInForeground() – Rougher Apr 22 '21 at 15:34
  • _"When in the foreground, the user is actively interacting with the application"_ -- that doesn't seem like a useful criterion. Taken literally, the _actual_ amount of time that a user is "actively interacting" is miniscule. User input involves only tiny fractions of CPU time to handle; for most programs, they are effectively idle well over 90% of the time. Why bother measuring at all? What _problem_ are you actually trying to solve here? That said, note that you could track window activation (i.e. a process window brought to the foregound), note the CPU time when activated and ... – Peter Duniho Apr 25 '21 at 18:12
  • ... deactivated, and subtract to get the total CPU time used while active. It's not clear what you actually mean by "foreground" or "actively interacting" here, but that's a possible approach, depending on what your question actually does mean. – Peter Duniho Apr 25 '21 at 18:12
  • Hi @PeterDuniho, you are right. Actually, I need to create a report with time spent in each application per hour/per day/per week. – Rougher Apr 26 '21 at 07:48

1 Answers1

1

It sounds like you want to monitor end-user application use. For that I'd recommend a global hook, however, global hooks in .Net are either extremely limited or blocked. Here's a old project that uses a C++ DLL from .Net to create the actual hook: https://www.codeproject.com/articles/6362/global-system-hooks-in-net

I don't think there is a hook for when a Application / Window becomes active though. So what you might want to do is add a Mouse Hook, and then use something like http://pinvoke.net/default.aspx/user32/GetForegroundWindow.html to get the handle of the current Active Foreground Window.

Compare that handle to the last handle you retrieve. If different, record a start date/time, and then use other functions get more info about the Window in question (such as Title bar and exe name).

Then every time you detect a change, record a end date/time, calculate the difference between the start / end time, and put that into a Dictionary<Handle, Long> that tracks total ticks for each Handle.

One caveat is Alt+Tab. You might need a keyboard hook as well.


One alternative to maybe check out is this article I found: https://www.c-sharpcorner.com/article/active-application-watcher-in-net-using-windows-forms/

Seems to also be doing what you want, but instead of it being a Mouse / Keyboard hook, it just has a Timer that fires every half second, and grabs the active foreground Window. The downside of that approach is accuracy (course even the hook isn't going to 100% precise).

If you go for the latter, I'd recommend swapping out the Timer control for something else. The System.Windows.Forms.Timer class is not precise - so it might fire every half second, but it might not - especially if the CPU Usage spikes cause of another app. Course other types of Timers are going to have issues with CPU Usage spikes, but Forms.Timer is probably the least precise of all the options available.

B.O.B.
  • 704
  • 4
  • 10
  • ...if you are trying to build something because management wants to monitor end-user behavior (big brother), it might be better to just buy and pay for a big brother application. I don't have any to recommend, and this is just a random one I googled for: https://www.microfocus.com/en-us/products/end-user-monitoring/overview <- something like that. Might be way cheaper / less of a headache long run to buy something vs writing in-house. Course that's all assuming this is something you are building for in-house use vs trying to create your own product to compete with the other apps I mentioned. – B.O.B. Apr 27 '21 at 20:58
  • Thanks for the detailed answer. Actually, my app is Windows Service. I think to use this solution (https://stackoverflow.com/questions/17345202/get-the-current-active-application-name) with a Timer (every second - it's enough precise for me). I only doubt about load on a client computer. What do you think? – Rougher Apr 28 '21 at 06:18