Q&A

  • Application.ProcessMessages 를 사용하는 이유는 먼가요?
프로그램에서 Application.ProcessMessages을 사용해서 코딩을 하는데
이 정확한 작동원리가 궁금합니다.
고수님들의 답변기다립니다.
2  COMMENTS
  • Profile
    김규한 2002.04.03 20:14
    ProcessMessage란 ,
    이 메소드를 호출함으로써 윈도우가 현재 메시지 큐에 있는 메시지를 처리
    할 수 있도록 한다구 나와 있군요.
    ProcessMessage는 윈도우 메시지 큐에 아무것도 남지 않을 때까지
    루프를 돌면서 메시지를 처리하고 모든 처리가 끝나면 제어권을 어플리케이션
    에 넘긴다라구도요 ^^;
    F1눌러서 도움말 확인하세요~


  • Profile
    장지용 2002.04.03 07:42

    이걸로 직접 테스트 해보시면 아실거예요.

    DELPHI NOTE:

    Watch out, Delphi is different, your code can fool you.

    Say, you want to hit a Button that turns a Panel Red for 3 seconds, then turns it to Green
    So you write the OnClick event for Button1:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Panel1.Color:=clRed;
      sleep(3000); // delays 3000 msec
      Panel1.Color:=clGreen;
    end;
    -----

    You Run the program, Click the Button; What happens?

    Nothing happens for 3 seconds then the Panel turns Green.
        What happened to Red?

    Hah, I say.

    Well guess what, when you are in the Button1Click procedure, you do not repaint the Form UNTIL YOU EXIT the procedure.

    While you are in the procedure, you see nothing changing on your Form; unless you say the magic words!
      Application.ProcessMessages;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      Panel1.Color:=clRed;
      Application.ProcessMessages; // this will repaint your Form
      sleep(3000);
      Panel1.Color:=clGreen;
    end;
    Now you hit the Button, the Panel turns Red for 3 seconds, then turns Green.

    So, what have we learned form this.

    If you are inside a procedure and you want things to change on your Form, so that the user can see them; you have to use:
        Application.ProcessMessages;

    At all the places where you want the Form to change.

    This gives you a simple way of doing quasi MULTITASKING in Delphi.