?
Solved

Detecting idle state

Posted on 2000-04-21
9
Medium Priority
?
292 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
Independent Software Vendors: 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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

800 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