Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Detecting idle state

Posted on 2000-04-21
9
Medium Priority
?
299 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

647 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