大家都知道PostMessage會丟消息,但是消息隊列的大小是多少呢,下面做了一個測試。
代碼:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls;
8
9 const
10 UM_ADD = WM_USER + 100;
11
12 type
13 TForm1 = class(TForm)
14 Label1: TLabel;
15 procedure FormCreate(Sender: TObject);
16 private
17 FVal: Integer;
18 procedure OnMsgAdd(var Msg: TMsg); message UM_ADD;
19 public
20 { Public declarations }
21 end;
22
23 var
24 Form1: TForm1;
25
26 implementation
27
28 {$R *.dfm}
29
30 procedure TForm1.FormCreate(Sender: TObject);
31 var
32 i: Integer;
33 begin
34 for i:=0 to 1000000 do
35 PostMessage(Handle, UM_ADD, 0, 0);
36 end;
37
38 procedure TForm1.OnMsgAdd(var Msg: TMsg);
39 begin
40 Inc(FVal);
41 Label1.Caption := IntToStr(FVal);
42 end;
43
44 end.
XP下運行結果:
可見,消息隊列的默認長度是10000(在WIN7下運行結果是9998),隊列滿后消息將被丟棄。
如果把代碼中的PostMessage改成SendNotifyMessage,運行后過了一段時間,窗口彈出:
所以,功能類似的SendNotifyMessage不會丟掉消息,必要時可以用它代替PostMessage。
上面的結論是錯誤的,SendNotifyMessage並沒有那么神奇,上面的例子沒有丟失消息的原因是因為
SendNotifyMessage發消息給同線程創建的窗口時,采用的是直接調用窗口函數的方式,換句話說
就是在執行SendMessage。
http://www.cnblogs.com/ddgg/archive/2013/03/31/2991595.html

