5

I need user idle detection in my application only (per "my application instance").
I can't use GetLastInputInfo because is session-specific user input:

GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function.

Any suggestions ?

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
halorty
  • 644
  • 1
  • 7
  • 11
  • 1
    what kind of application is that? Windows Application? you have it running on multiple computers? what do you want to get? – Davide Piras Feb 15 '11 at 14:06
  • Yes, **Windows Application**. It can be running on the same machine on the same "Windows User" – halorty Feb 15 '11 at 14:17
  • When you say "per application instance", do you mean checking for idle on all running applications in the system, or just your own application? –  Feb 15 '11 at 14:18
  • Just **my own application** only – halorty Feb 15 '11 at 14:20

3 Answers3

5

I assume you want to detect the idle time but only when you application is active. You then have to define exactly what you mean by your application being active. When your application is active (by your own definition) you can at regular intervals call GetLastInputInfo to determine the idle time of your application when it is active (e.g. using a timer of some sort).

Windows has a concept of a foreground window and the current foreground window can be retrived using GetForegroundWindow. You can use GetWindowThreadProcessId to find the process ID of the process owning the foreground window. If that process ID is your process ID you know that you are the foreground process even if your application has multiple windows. You will have to do this detection at regular intervals just as you have to check the idle time.

You should not be concerned about GetLastInputInfo only providing information about the session. If multiple users are logged onto the same Windows computer they will each have their own session, but another user being idle or not ilde in a session should not affect how you detect if the user of your application is idle.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
  • I want to detect the idle time when my application is active and inactive. I mean when is minimlized, when maxmalized but user works with oter application, when user works with my application (active) but doing nothing (not using mouse, keyboard, etc.) – halorty Feb 16 '11 at 07:41
  • When my application is active (by my own definition, above) I can't use _GetLastInputInfo_ at regular intervals, for example when my app is minimlized (in my own definition, my app is active). – halorty Feb 16 '11 at 07:47
4

You could use Application.AddMessageFilter and watch for messages representing user interaction (e.g. mouse, keyboard, maybe menu activation).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

If this is a Windows Forms application, you can create an idle event handler and subscribe to the Application.Idle event. You'll get a notification when the application finishes processing and is about to enter the idle state.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • I need notification on user idle, not application idle. My application has a lot of background processing taks. – halorty Feb 16 '11 at 07:35
  • @halorty: As far as `Application.Idle` is concerned, "idle" is user interface idle. When the UI thread is done processing and about to enter the idle state, you get the `Application.Idle` event. Stuff happening on background threads isn't going to change that. – Jim Mischel Feb 16 '11 at 14:53