Link to home
Start Free TrialLog in
Avatar of MoonCalf
MoonCalf

asked on

Application.ProcessMessages

Hi.

I've written a small application that basically runs an executable, changes the title, and then keeps checking to see if the title needs changing again.  (Users need to have specific information in the titlebar which changes regularly.)

It works really nice, but I need to keep it as small as possible.  Therefore, I'd like to remove "Forms" from my uses clause and just leave Windows in there, the only other unit I need to reference.

How can I acheive "Application.ProcessMessages" type functionality, without using that command.

Thanks,

MoonCalf.
Avatar of Jaymol
Jaymol

Listening with great interest. : )
Hi MoonCalf,

may be attached sample will be usefull for you.

-----
Igor.

program MonitorePcs;

uses
  windows,
  classes,
  sysutils;

type
  TIdleMonitor = class(TObject)
  private
    FStop: Boolean;
  public
    procedure Run;
  end;

procedure TIdleMonitor.Run;
begin
  PostMessage(0, 0, 0, 0);
  repeat
    WaitMessage;
    //
    // Do something here.
    // Do not call PeekMessage, otherwise loop will be stopped
    //
    Beep;   // for example
  until FStop;
end;


var
  RunObject: TIdleMonitor;

begin

  RunObject := TIdleMonitor.Create;
  RunObject.Run;
  RunObject.Free;

end.
Avatar of MoonCalf

ASKER

Thanks ITugay, but how would that help me?  I don't understand how to use that instead of "Application.ProcessMessages"

MoonCalf.
This is the forms-less equivalent to Application.ProcessMessages. However, if you run this function in a loop with no "Sleep" in it, your program will push the CPU to %100, same with Application.ProcessMessages. So you should perhaps think about replacing "PeekMessage" with "GetMessage".

procedure ProcessMessages;
var msg : TMsg;
begin
  while PeekMessage(msg, 0, 0, 0, PM_REMOVE) do begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
end;

Regards, Madshi.
My application is not tying anything else up, but ProcessMessages doesn't alter the fact that I'm using 99% of the CPU.  Can this be improved in any way?

MoonCalf.
Hi MoonCalf,

as I guessed you need to do some operation periodically and let time to application to perform it's deals. Right?

WaitMessage not only checks message queue, but let to run another applications. By another words its analog of Application.ProcessMessages.

-----
Igor.

PS: WaitMessage doesn't returns control to application while another applications have to do something.

Hi MoonCalf,

you can do some work by timer to avoid of 99%.
In this case you do not need to put any messages to the queue before calling WaitMessage.

-----
Igor.

Sorry Igor, but I don't understand.  Could you give an example please.

MoonCalf.
ASKER CERTIFIED SOLUTION
Avatar of Igor UL7AAjr
Igor UL7AAjr
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Igor, you the man.

Thanks