Application.HandleMessage与Application.ProcessMessage

古城微笑少年丶 2021-12-12 06:47 246阅读 0赞

HandleMessage:

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

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

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

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

ProcessMessage:

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

ContractedBlock.gif ExpandedBlockStart.gif

  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls;
  6. type
  7. TForm1 = class(TForm)
  8. Button1: TButton;
  9. Button2: TButton;
  10. procedure FormCreate(Sender: TObject);
  11. procedure Button1Click(Sender: TObject);
  12. procedure Button2Click(Sender: TObject);
  13. procedure FormShow(Sender: TObject);
  14. private
  15. {
  16. Private declarations }
  17. procedure MyIdleHandler(Sender: TObject; var Done: Boolean);
  18. public
  19. {
  20. Public declarations }
  21. end;
  22. var
  23. Form1: TForm1;
  24. implementation
  25. {
  26. $R *.dfm}
  27. var
  28. {
  29. Global variables to show the order of events }
  30. XPos, YPos, Delta: integer;
  31. procedure StatusMsg(
  32. MyForm : TForm1; Canvas : TCanvas; Message : string; Bkg : Boolean);
  33. begin
  34. if not bkg then
  35. Canvas.Font.Style := [fsBold]; {
  36. Foreground messages are in bold type. }
  37. Canvas.TextOut(XPos, YPos, Message);
  38. if not bkg then
  39. Canvas.Font.Style := [];
  40. {
  41. Change Xpos and YPos to prepare for the next message. }
  42. Ypos := Ypos + Delta;
  43. if YPos >= MyForm.ClientHeight - 10 then
  44. begin
  45. YPos := 10;
  46. Xpos := Xpos + 180;
  47. end;
  48. if (Xpos >= MyForm.ClientWidth - 100) then
  49. begin
  50. if (Canvas.Font.Color = clRed) then
  51. Canvas.Font.Color := clBlack
  52. else
  53. Canvas.Font.Color := clRed;
  54. Xpos := 10;
  55. end;
  56. end;
  57. procedure TForm1.FormCreate(Sender: TObject);
  58. begin
  59. Button1.Caption := 'Do not yield';
  60. Button2.Caption := 'Handle Message';
  61. Application.OnIdle:= MyIdleHandler;
  62. XPos := 10;
  63. YPos := 10;
  64. Delta := Abs(Canvas.Font.Height) + 1;
  65. end;
  66. procedure TForm1.Button1Click(Sender: TObject);
  67. var
  68. I, J, X, Y: Word;
  69. begin
  70. StatusMsg(
  71. Form1, Canvas, 'The synchronous handler is starting', False);
  72. I := 0;
  73. J := 0;
  74. while I < 10 do
  75. begin
  76. Randomize;
  77. while J < 10 do
  78. begin
  79. Y := Random(J);
  80. Inc(J);
  81. end;
  82. X := Random(I);
  83. Inc(I);
  84. end;
  85. StatusMsg(
  86. Form1, Canvas, 'The synchronous handler is done', False);
  87. end;
  88. procedure TForm1.Button2Click(Sender: TObject);
  89. var
  90. I, J, X, Y: Word;
  91. begin
  92. //同时执行onIdle事件和当前任务
  93. StatusMsg(
  94. Form1, Canvas, 'The asynchronous handler is starting', False);
  95. I := 0;
  96. J := 0;
  97. while I < 100 do
  98. begin
  99. Randomize;
  100. while J < 10 do
  101. begin
  102. Y := Random(J);
  103. Inc(J);
  104. end;
  105. X := Random(I);
  106. Inc(I);
  107. {
  108. yield to OnIdle or other messages }
  109. // PostMessage(Handle,WM_PAINT,0,0); 假如消息队列不是空,则不会生成onIdle事件
  110. Application.HandleMessage;
  111. end;
  112. StatusMsg(
  113. Form1, Canvas, 'The asynchronous handler is done', False);
  114. end;
  115. procedure TForm1.FormShow(Sender: TObject);
  116. begin
  117. end;
  118. {
  119. TForm1 }
  120. procedure TForm1.MyIdleHandler(Sender: TObject; var Done: Boolean);
  121. begin
  122. StatusMsg(
  123. Form1, Canvas, 'This represents a background process.', True);
  124. end;
  125. end.

转载于:https://www.cnblogs.com/Coder-MIFir/p/11075669.html

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读

    相关 RAMROMCMOS

    静态RAM常用作高速缓存。 CPU的速度不断提高也大大超过了内存的速度,使得CPU在进行数据存储时需要等待,从而降低了整个计算机系统的运行速度,为解决这一问题引入了 cache

    相关 |||,&&&区别

    &,&&:(与,短路与):一样的地方就是二者执行最后的结果是一样的,但是执行的过程有区别, 对于&:无论&左边是否为false,他都会继续检验右边的boolean值。 对于