Jlouro
Jlouro

Reputation: 4545

Cancel idle state by code…

I am detecting the machine Idle state and if Idle, I perform an action.

One of the actions must be canceling this state otherwise it will enter a loop.
I want to prevent re-entry into my idle loop when I'm executing in it, how do I do this?

Upvotes: 1

Views: 555

Answers (3)

Johan
Johan

Reputation: 76537

Add a flags in your form's vars that tests to see if you're already in an idle loop:

interface

type TForm1 = class(TForm)
...
private
...
  InIdleLoop: boolean;
...

implementation

procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
  if InIdleLoop then exit;
  InIdleLoop:= true
  try
   //Do your idle processing here

  finally
    InIdleLoop:= false;
  end; {try}
end;

Upvotes: 1

Heinz Z.
Heinz Z.

Reputation: 1574

One way to prevent re-entrance is to memorize if you have entered the event handler:

type
  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
  private
    { Private-Deklarationen }
    FInOnIdle: Boolean;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
begin
  if FInOnIdle then Exit;

  FInOnIdle := True;
  try
    
  finally
    FInOnIdle := False;
  end;
end;

Upvotes: 5

opc0de
opc0de

Reputation: 11767

By pressing a key i guess this will do the trick

  keybd_event(VK_SHIFT,MapVirtualKey(VK_SHIFT,0),0,0);
  keybd_event(VK_SHIFT,MapVirtualKey(VK_SHIFT,0),KEYEVENTF_KEYUP,0);

This presses the shiftkey and the system will think the computer isn't idle any more...

Upvotes: 0

Related Questions