RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TApplication.ProcessMessages Method

Interrupts the execution of an application so that it can process the message queue.

Pascal
procedure ProcessMessages;
C++
__fastcall ProcessMessages();

Call ProcessMessages to permit the application to process messages that are currently in the message queue. ProcessMessages cycles the Windows message loop until it is empty, and then returns control to the application.

Note: Neglecting message processing affects only the application calling ProcessMessages, not other applications. In lengthy operations, calling ProcessMessages periodically allows the application to respond to paint and other messages.
Note: ProcessMessages does not allow the application to go idle, whereas HandleMessage does.
 

C++ Examples: 

 

/*
This example uses two buttons that are long enough to 
accommodate lengthy captions on a form. When the user clicks
the button with the caption Ignore Messages, the code begins
to generate a long series of random numbers. If the user 
tries to resize the form while the handler is running, 
nothing happens until the handler is finished. When the user
clicks the button with the caption Process Messages, more 
random numbers are generated, but the application can still 
respond to a series of events, such as resizing the form
and printing text.
Note: How quickly these event handlers run depends on the 
microprocessor of your computer. A message appears on the 
form informing you when the handler has finished executing.
*/

int magicnumber = 20;

void __fastcall TForm1::FormCreate(TObject* Sender)
{
  Button1->Caption = "Ignore Messages";
  Button2->Caption = "Handle Message";
}

void __fastcall TForm1::Button1Click(TObject* Sender)
{
  int x, y;
  Canvas->TextOut(10, 10, "The Button1Click handler has started");
  Application->ProcessMessages(); // Just to get out the
  Sleep(100);
  for (int i = 0; i < magicnumber; i++)
 {
    Randomize();
    for (int j = 0; j < magicnumber; j++)
    {
      Sleep(10);
      y = random(j);
    }
    x = random(i);
  }
  Canvas->TextOut(10, 10, "The Button1Click handler is finished   ");
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  int x, y;
  Canvas->TextOut(10, 10, "The Button2Click handler has started");
  for (int i = 0; i < magicnumber; i++)
  {
    Randomize();
    for (int j = 0; j < magicnumber; j++)
    {
      y = random(j);
      Sleep(10);
      Application->ProcessMessages();
    }
    x = random(i);
  }
  Canvas->TextOut(10, 10, "The Button2Click handler is finished   ");
}

 

Delphi Examples: 

{
This example uses two buttons that are long enough to 
accommodate lengthy captions on a form. When the user clicks
the button with the caption Ignore Messages, the code begins
to generate a long series of random numbers. If the user 
tries to resize the form while the handler is running, 
nothing happens until the handler is finished. When the user
clicks the button with the caption Process Messages, more 
random numbers are generated, but the application can still 
respond to a series of events, such as resizing the form
and printing text.
Note: How quickly these event handlers run depends on the 
microprocessor of your computer. A message appears on the 
form informing you when the handler has finished executing.
}
const magicnumber = 500;
procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Caption := 'Ignore Messages';
  Button2.Caption := 'Process Messages';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I, J: Integer;
  X, Y: Word;
begin
  I := 0;
  J := 0;
  Canvas.TextOut(10, 10, 'The Button1Click handler has started');
  Application.ProcessMessages; // Just to get the message out.
  while I < magicnumber do
  begin
    Randomize;
    while J < magicnumber do
    begin
      Sleep(10);
      Y := Random(J);
      Inc(J);
    end;
    X := Random(I);
    Inc(I);
  end;
  Canvas.TextOut(10, 10, 'The Button1Click handler is finished   ');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  I, J: Integer;
  X, Y: Word;
begin
  I := 0;
  J := 0;
  Canvas.TextOut(10, 10, 'The Button2Click handler has started');
  while I < magicnumber do
  begin
    Randomize;
    while J < magicnumber do
    begin
      Y := Random(J);
      Inc(J);
      Sleep(10);
      Application.ProcessMessages;
    end;
    X := Random(I);
    Inc(I);
  end;
  Canvas.TextOut(10, 10, 'The Button2Click handler is finished   ');
end; 

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!