zoukankan      html  css  js  c++  java
  • Application.HandleMessage与Application.ProcessMessage

    HandleMessage:

        HandleMessage中断应用程序的执行,以便Windows可以在将控制权返回给应用程序之前处理来自Windows消息队列的单个消息。

                  如果消息队列为空HandleMessage生成OnIdle事件并启动更新应用程序中的操作的过程

        注意:如果应用程序空闲,HandleMessage可能需要很长时间才能返回。

        因此,在等待基于消息的事务时,不要在处理优先级操作时调用HandleMessage。 相反,在处理的不仅仅是消息时调用ProcessMessages。

    ProcessMessage:

             ProcessMessages不允许应用程序空闲,而HandleMessage则允许。

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
      private
        { Private declarations }
        procedure MyIdleHandler(Sender: TObject; var Done: Boolean);
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    var
    { Global variables to show the order of events }
      XPos, YPos, Delta: integer;
    
    
    procedure StatusMsg(
      MyForm : TForm1; Canvas : TCanvas; Message : string; Bkg : Boolean);
    begin
      if not bkg then
        Canvas.Font.Style := [fsBold]; {Foreground messages are in bold type. }
      Canvas.TextOut(XPos, YPos, Message);
      if not bkg then
        Canvas.Font.Style := [];
      { Change Xpos and YPos to prepare for the next message. }
      Ypos := Ypos + Delta;
      if YPos >= MyForm.ClientHeight - 10 then
      begin
        YPos := 10;
        Xpos := Xpos + 180;
      end;
      if (Xpos >= MyForm.ClientWidth - 100) then
      begin
        if (Canvas.Font.Color = clRed) then
          Canvas.Font.Color := clBlack
        else
          Canvas.Font.Color := clRed;
        Xpos := 10;
      end;
    end;
    
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      Button1.Caption := 'Do not yield';
      Button2.Caption := 'Handle Message';
      Application.OnIdle:= MyIdleHandler;
      XPos := 10;
      YPos := 10;
      Delta := Abs(Canvas.Font.Height) + 1;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I, J, X, Y: Word;
    begin
      StatusMsg(
        Form1, Canvas, 'The synchronous handler is starting', False);
      I := 0;
      J := 0;
      while I < 10 do
      begin
        Randomize;
        while J < 10 do
        begin
          Y := Random(J);
          Inc(J);
        end;
        X := Random(I);
        Inc(I);
      end;
      StatusMsg(
        Form1, Canvas, 'The synchronous handler is done', False);
    
    end;
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
      I, J, X, Y: Word;
    begin
    //同时执行onIdle事件和当前任务
     StatusMsg(
        Form1, Canvas, 'The asynchronous handler is starting', False);
      I := 0;
      J := 0;
      while I < 100 do
      begin
        Randomize;
        while J < 10 do
        begin
          Y := Random(J);
          Inc(J);
        end;
        X := Random(I);
        Inc(I);
        { yield to OnIdle or other messages }
    //    PostMessage(Handle,WM_PAINT,0,0);  假如消息队列不是空,则不会生成onIdle事件
        Application.HandleMessage;
      end;
      StatusMsg(
        Form1, Canvas, 'The asynchronous handler is done', False);
    
    end;
    
    procedure TForm1.FormShow(Sender: TObject);
    begin
    
    end;
    
    { TForm1 }
    
    procedure TForm1.MyIdleHandler(Sender: TObject; var Done: Boolean);
    begin
        StatusMsg(
        Form1, Canvas, 'This represents a background process.', True);
    end;
    
    end.
    View Code
  • 相关阅读:
    pandas高效实现条件逻辑
    Python教程:文件、异常处理和其他
    最终初学者指南,以数据科学用例赢得分类黑客马拉松
    用Nvidia Jetson Nano 2GB和Python构建一个价值60美元的人脸识别系统
    一幅图像能顶16x16字!——用于大规模图像缩放识别的变压器(对ICLR 2021年论文的简要回顾)
    接缝雕刻算法:一种看似不可能的图像大小调整方法
    apache与nginx的优缺点的比较
    php5与php7的区别
    git基本的命令大全
    redis和membercache的区别
  • 原文地址:https://www.cnblogs.com/Master-Qi/p/11075669.html
Copyright © 2011-2022 走看看