在线程中怎样处理消息?谢谢大家!

mmtang3 2003-02-18 05:51:09
谢谢!
...全文
106 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
纯冰糖 2003-02-19
  • 打赏
  • 举报
回复
厉害,收了
lxpbuaa 2003-02-18
  • 打赏
  • 举报
回复
一个简单的例子:
unit Unit2;

interface

uses
Classes, Messages, Windows;

type
MyTh = class(TThread)
private
CanTermited: Boolean;
procedure NotificationWndProc(var Message: TMessage);
protected
procedure Execute; override;
public
MHandle: HWnd;
constructor Create(CreateSuspended: Boolean; CanRMemmage: Boolean = True);overload;
destructor Destroy;override;
end;

implementation


procedure MyTh.Execute;
begin
while not CanTermited do
end;

constructor MyTh.Create(CreateSuspended: Boolean; CanRMemmage: Boolean = True);
begin
inherited Create(CreateSuspended);
CanTermited := False;
if CanRMemmage then
begin
MHandle := Classes.AllocateHWnd(NotificationWndProc);
end;
end;

destructor MyTh.Destroy;
begin
if MHandle <> 0 then
Classes.DeallocateHWnd(MHandle);
inherited;
end;

procedure MyTh.NotificationWndProc(var Message: TMessage);
begin
CanTermited := Message.Msg = WM_CLOSE;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
MT := MyTh.Create(True, True);
MT.Priority := tpLower;
MT.Resume;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if PostMessage(MT.MHandle, WM_CLOSE, 0, 0) then
ShowMessage('succeeds');
end;
线程自己建立了一个不可见窗口用来接收消息,然后将消息发给这个窗口.

—————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
—————————————————————————————————
lxpbuaa 2003-02-18
  • 打赏
  • 举报
回复
线程自己必须建立消息队列;用PostThreadMessage给线程发送消息,用GetMessage 或者PeekMessage 检索消息。

—————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
—————————————————————————————————

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧