How to tell when to time-out

How to tell when to time-out

How can I determine that a user has been inactive for a certain length of time so that I may exit the application?

This is an interesting question because it introduces one form of securityfor an application, and that is time-sensitive, user activity-basedprocessing. This type of “awareness” in a program can usually be found indialup programs (CompuServe, MSN), but they also in security log-ins, in which if a response is not made within a discrete periodof time, the program will close and you’ll have to start all over again.

InDelphi, this is pretty easy to implement. What you’re about to see is not the prettiest solution in the world, but it works.

Here’s how to implement user activity-based time-sensitivity in your programs:

  1. In the main form of your application, set the KeyPreview property to Trueso the form will see keystrokes before any other components (you’llsee why when you see the code for the OnKeyDownmethod). And in the FormCreate method, write the following code:
    procedure TForm1.FormCreate(Sender: TObject);begin  TimeOut := 0;end;
  2. Ddrop a Timer component on the form and set its interval to 1000milliseconds (the default).
  3. Switch to the editor. Under the implementation section, declare thefollowing const and var:
    const  MaxTimeOutValue = 300; {This is 300 seconds, or five minutes}var  TimeOut : Integer; {This will be incremented by the Timer}
  4. Write the following procedure and declare in the private section of yourform:
    procedure TForm1.ResetTimeOut;begin  TimeOut := 0;end;
  5. Open up the OnKeyDown method for your form and put the following code:
    procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);begin  ResetTimeOut;end;
    This calls ResetTheTimer which, in turn, resets the TimeOutvariable to zero, then disables and re-enables the Timer. The reason we doform-level processing of the keystrokes is so that no matter which componentthe user is typing in, keystrokes are always picked up by theform. Otherwise, you’d have to add this code to every component, and if youhave a lot on your form … yikes! I’d rather not think about it
  6. In the OnTimer event of the Timer, put the following code:
procedure TForm1.Timer1Timer(Sender: TObject);begin  Inc(TimeOut);  if TimeOut = MaxTimeOutValue then begin    Timer1.Enabled := False;    Close;  end;end;
This increments the TimeOut variable and compares it against theMaxTimeOutValue. If TimeOut equals MaxTimeOutValue, the timer isdisabled and Close is called.

So how does this all work?

When the user presses a key while the form is running, TimeOut is reset to 0. This means that if the user is constantly typing, there’s noway TimeOut can ever reach MaxTimeOutValue. However, once the user stops typing,because the timer is always enabled, TimeOut will be incremented every second,and will eventually reach the value equivalent to MaxTimeOutValue.

This isn’t pretty, but it works.

See also  Monetize TikTok For Your Business

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist