CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    Terminating an app after 5 min idle ?

    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.

    One good reference to get me started was:
    http://www.codeguru.com/forum/showth...ight=idle+time

    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.

    Any help greatly appreciated.
    mpliam

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Terminating an app after 5 min idle ?

    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.

    Is there anything that I'm missing here?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: Terminating an app after 5 min idle ?

    Create a thread using CreateThread.
    In the thread create a waitable timer - Using Waitable Timer Objects

    Set the signal time to 5 mins or what ever needed.
    When the timer is signalled, poll using GetLastInputInfo.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    May 2002
    Posts
    1,798

    Re: Terminating an app after 5 min idle ?

    Thank you for your suggestions. But the problem is that the idle time must only apply to the application in question, not the entire workstation.

    If you wish, take a look at the little program I have attached and you will see the problem immediately.
    Attached Files Attached Files
    mpliam

  5. #5
    Join Date
    May 2002
    Posts
    1,798

    Re: Terminating an app after 5 min idle ?

    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.

    Now, Microsoft says on GetLastInputInfo ...
    http://msdn.microsoft.com/en-us/libr...02(VS.85).aspx
    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.

    The real key to my understanding of this problem came from GCDEF.
    http://www.codeguru.com/forum/printthread.php?t=352265
    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.
    Attached Files Attached Files
    Last edited by Mike Pliam; September 22nd, 2009 at 07:05 PM. Reason: question arises
    mpliam

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Terminating an app after 5 min idle ?

    In CTerminateOptionDlg constructor you initialize
    Code:
    m_fLastAppInput = 0.0;
    Then, in the OnTimer function you sai:
    Code:
    m_fAppIdle = clock() - m_fLastAppInput;
    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).
    Code:
    m_fLastAppInput = clock();
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    May 2002
    Posts
    1,798

    Re: Terminating an app after 5 min idle ?

    Cilu,

    I really cannot thank you enough for taking the time to solve my problem for me. I don't know how long it might have taken me to figure it out.

    mpliam

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured