We help IT Professionals succeed at work.

Application.ProcessMessages

MoonCalf
MoonCalf asked
on
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.
Comment
Watch Question

Commented:
Listening with great interest. : )
Igor UL7AAjrSenior developer

Commented:
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.

Author

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

MoonCalf.

Commented:
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.

Author

Commented:
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.
Igor UL7AAjrSenior developer

Commented:
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.

Igor UL7AAjrSenior developer

Commented:
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.

Author

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

MoonCalf.
Senior developer
Commented:
Hi MoonCalf,

here is modified sample with timer.

It's easy. While WaitMessage executed and no messages in queue processor doesn't looks like busy by your application and your thread waits for message. Once timer message occurs, you just need to put some message to messages queue and process it in your message loop.

program MonitorePcs;

uses
  windows,
  messages,
  classes,
  sysutils;

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

procedure TIdleMonitor.Run;
var
  M: TMsg;
begin
  repeat
    WaitMessage;
    PeekMessage(M, 0, 0, 0, PM_REMOVE);
    if M.Message = WM_TIMER then
    begin
      //
      // Do something here.
      // Do not call PeekMessage, otherwise loop will be stopped
      //
      Beep;   // for example
    end;
  until FStop;
end;


procedure TimerProc(HWnd, uMsg, idEvent, Time: Integer); stdcall;
begin
  PostMessage(0, WM_TIMER, 0, 0);
end;


var
  RunObject: TIdleMonitor;
  Timer: Integer;

begin

  RunObject := TIdleMonitor.Create;
  Timer := SetTimer(0, 1, 500, @TimerProc);
  RunObject.Run;
  KillTimer(0, Timer);
  RunObject.Free;

end.

Author

Commented:
Igor, you the man.

Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.