ProcessMessages (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses two buttons that are long enough to accommodate lengthy captions on a form. When you click the button with the caption Ignore Messages, the code begins to generate a long series of random numbers. If you try to resize the form while the handler is running, nothing happens until the handler is finished. When you click 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;

Code

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  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(); // Force redraw.
  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   ");
}

Uses