How to tell when to time-out

How to tell when to time-out

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

Answer:
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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of