Now that you have helped me to programatically terminate an application, http://www.codeguru.com/forum/showthread.php?t=484775
I now need to take the next step which is to terminate an application after an elapsed idle time of 5 minutes.
And here is a little console app that I wrote on the basis of that thread:
Code:
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#pragma comment(lib, "user32.lib")
using namespace std;
int main()
{
int n;
double tickCount;
double idleCount;
LASTINPUTINFO lpi;
lpi.cbSize = sizeof(LASTINPUTINFO);
for(;;)
{
cout << "Enter number : ";
cin >> n;
if (n == 0) break;
if (!GetLastInputInfo(&lpi)) { }// failed, use GetLastError to get error code
// lpi.dwTime now holds the tick count when last input was made
tickCount = GetTickCount();
idleCount = ( tickCount - lpi.dwTime );
cout << "tickCount = " << tickCount << endl;
cout << "lpi.dwTime = " << lpi.dwTime << endl;
cout << "idleCount = " << idleCount << endl;
}
cout << "That's all folks!" << endl << endl;
return 0;
}
Now the logical next step is to incorporate these ideas into an application timer that can be set to terminate the application at a specified (say 5 min) idle time. However, should the user interrupt the idle time with some action (application specific keyboard, mouse click, etc), then the timer should be reset.
The purpose of this application is to run on a network server share. To prevent simultaneous access of the application's database, the application has been configured to run a single instance at a time (see: http://support.microsoft.com/kb/243953). Should some user walk away from their workstation leaving the application open, the app would no longer be accessible from any other workstation on the network.
My current dilemma is that I am uncertain how to link the GetLastInputInfo() method with a timer that fulfills the above criteria.
As far as I see you need to periodically poll for the last input time. When that last time is greater than 5 minutes, than you close the application. You can use a timer for this polling. You don't have to reset this timer, because is not a timer for the idle time, but a timer for the polling operation.
This is really such an interesting and practical problem that I thought I would ruminate a bit more on it.
Cilu wrote:
As far as I see you need to periodically poll for the last input time. When that last time is greater than 5 minutes, than you close the application. You can use a timer for this polling. You don't have to reset this timer, because is not a timer for the idle time, but a timer for the polling operation.
Understanding this was one key to the solution. The timer is not actually calculating the idle time but rather checking at periodic intervals (polling) to see what the idle time is.
This function is useful for input idle detection. However, 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.
I found this to be unclear and misleading. The GetLastInputInfo clearly helps one to establishe when the last SYSTEM input occur. Obviously, this also includes the application specific input, but not exclusively.
Re: Fine the Idle Time in a Application
I don't know about touch screen apps, but I do it in regular windows apps by using the app's PreTranslateMessage function and looking for and recording the time of the last WM_KEYDOWN or WM_MOUSEMOVE indicating some kind of user activity. Then you can set up a timer to go off every few seconds and check the last activity time against the current time. Based on that number, you can take whatever action you want.
For those who might wish to program an application suitable to run on a network share, having an application specific idle time detection and automatic application termination based on that time, I have included a dialog-based demo program that seems to do the job.
I have a question about the calibration of the timing. While the primary application timer which is set to trigger the termination option dialog at 20 seconds seems to be fairly consistent, unfortunately the termination dialog timer is not. It will sometimes initially indicate that there is 39 seconds left and sometimes only 3 seconds remaining. It would seem that something is affecting the termination dialog timer, but I can't figure out what. Any help on this matter greatly appreciated.
Last edited by Mike Pliam; September 22nd, 2009 at 07:05 PM.
Reason: question arises
That means m_fAppIdle will be time since the process started minus 0, which is the time since the process started. That means the second dialog will timeout depending on the last input in the first window. If you did something for 40 seconds and than stopped, this dialog will show up after 60 seconds. But as soon as the timer elapses and you check the dialog, you'll get m_fAppIdle greater than 60 seconds and close it. The key is to initialize m_fLastAppInput with the value clock() (either in constructor or in OnInitDialog).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.