Solved

Detecting idle state

Posted on 2000-04-21
9
291 Views
Last Modified: 2010-04-04
Can somebody give me a full example on how to install some sort of hook that will execute a function after 2 minutes of idle time ?
0
Comment
Question by:frenzyr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Expert Comment

by:SuperSy
ID: 2739200
I am not exactly sure about your definition of "idle time", but I am going to go ahead and assume that you mean that the your application is not doing anything.  (You could be moving the cursor or pressing the Shift key, as long as your application is doing nothing.)

1) Have a TTimer component in your application

(ex.)
....
type
  TForm1 = class(TForm)
  ...
  IdleTimer: TTimer;
  ...
end;
....

2) In your application's OnCreate, define your OnIdle handling procedure.

(ex.)
procedure TForm1.FormCreate(Sender: TObject);
begin
  ...
  Application.OnIdle:= MyIdleHandler;
  ..
end;

3) In your OnIdle handler, set the timer to two minutes when called. Set Done to True so that your CPU is not eaten up.

(ex.)
procedure TForm1.MyIdleHandler(Sender: TObject; var Done: Boolean);
begin
  Done := True;
  IdleTimer.Interval := 2 * 60 * 1000;
  // Two minutes in msec.
  IdleTimer.Enabled := True;
end;

4) Finally, in your OnTimer event handler for your Idle time timer, do what you want to do.

procedure TForm1.IdleTimerTimer(Sender: TObject);
begin
  IdleTimer.Enabled := False;
  IdleTimer.Interval := 0;
  ShowMessage(Two minutes idle time passed!');  
end;

This works...if your definition of "idle time" matches mine.  

^_^

SuperSy

0
 

Expert Comment

by:SuperSy
ID: 2739208
If you want to update the timer when a key is pressed or the mouse is moved, you can set an OnMessage handler just like the OnIdle handler.

In 2) above, add
  Application.OnMessage := MyMessageHandler;

and write your own handler like the one below.

procedure TForm1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
begin
  if (Msg.message= WM_KEYDOWN) or (Msg.message= WM_MOUSEMOVE) then
  begin
    IdleTimer.Interval := 2 * 60 * 1000;
    // Two minutes in msec.
    IdleTimer.Enabled := True;
  end;    
end;
0
 
LVL 1

Author Comment

by:frenzyr
ID: 2739567
Thank you for your comment, however i mean a system wide idle state, instead of an application wide one.

Can you plesae help me with that ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:bryan7
ID: 2740809
i have a demo of an app that monitors cpu usage..
you can use that .. if 2 minutes have happened with the cpu usage at 0...1 .. then do something
0
 
LVL 1

Accepted Solution

by:
msedi earned 200 total points
ID: 2750994
Hello,

I don't know if you still want an answer. Here's mine you should set up a windows hook in a dll, because only there the hook is global. After that you should install a hook for every event that comes up (e. g. messages, mouse and so on). Just set a timer if a message occurs and reset the timer every time another message occurs. If the time is the given time you can call your method. If I have time I will set up a demo.

Good luck, Martin
0
 
LVL 1

Author Comment

by:frenzyr
ID: 2754730
Thanks i've done that and its working now.
0
 

Expert Comment

by:clopez
ID: 2807150
I am searching for a solution for the same problem.  But I don't know how to implement it, could you explain futher please.

If you can post an example or send it to clopez@intercom.es

Thanks
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2809095
me too.. brian7@teleline.es

thanks
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2809097
me too.. brian7@teleline.es

thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question