Application.HandleMessage与Application.ProcessMessage
2024-10-22 04:47:29
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.
最新文章
- c#开发Mongo笔记第五篇
- 【转】MYSQL入门学习之九:索引的简单操作
- UVA 10152-ShellSort(映射+栈)
- Sass介绍及入门教程
- NASPhoto Station不只是储存的强大照片管理功能
- asp.net权限认证:摘要认证(digest authentication)
- WEB前端需要了解的XML相关基础知识
- 【Java每日一题】20170206
- LeetCode(83): 删除排序链表中的重复元素
- sql-datediff
- sql练习(针对Mysql)
- 9.C#知识点:线程初识及Thread初识(一)
- C#调用托管ocx、dll
- FineReport中如何用JavaScript自定义地图标签
- 使用TestNG 和 CSV文件进行数据驱动
- BPSK相干解调和DBPSK非相干解调误码率仿真
- js停止(阻止)浏览器继续加载内容
- Halcon函数【转】
- Django运行访问项目出现的问题:Invalid HTTP_HOST header: &#39;192.168.114.25:8001&#39;. You may need to add u&#39;192.168.114.25&#39; to ALLOWED_HOSTS.
- Spring Boot入门——JDBCTemplate使用及其相关问题解决
热门文章
- ASP.NET Core Filter如何支持依赖注入
- [Swift]使用Alamofire传递参数时报错
- ClickHouse 使用
- 2023-03-01 Warning: require(C:\wamp\www\tp5\public../thinkphp/base.php): failed to open stream: No such file or directory in C:\wamp\www\tp5\public\index.php on line 15
- Java的引用(强软弱虚)
- Day_1(并查集朋友圈、字典序排序)
- mybatis-plus 使用 sql 分页
- 解决不了ERROR: org.apache.hadoop.hbase.PleaseHoldException: Master is initializing啊
- js引入样式资源报错
- C 标准 C89/C90/C99/C11/C17