在线程中如何接收通过PostThreadMessage()发送的消息? - 程序开发常见问...
来源:百度文库 编辑:神马文学网 时间:2024/04/20 07:13:48
在线程中如何接收通过PostThreadMessage()发送的消息?
最好有实例!
// 用 PeekMessage 接收
// 一个例子
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,MyThread;
const
WM_MyMessage=WM_USER 100;
type
TForm1 = class(TForm)
BtnQuitThread: TButton;
ListBox1: TListBox;
BtnPost: TButton;
BtnStart: TButton;
BtnExit: TButton;
procedure BtnQuitThreadClick(Sender: TObject);
procedure BtnStartClick(Sender: TObject);
procedure BtnPostClick(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyThread : MsgThread;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.BtnQuitThreadClick(Sender: TObject);
begin
// MyThread.Terminate;
if MyThread = nil then exit;
if PostThreadMessage(MyThread.ThreadID,
WM_QUIT,0,0) then
Caption := ‘Post Message Ok!‘
else
Caption := ‘Post Message Fail!‘;
end;
procedure TForm1.BtnStartClick(Sender: TObject);
begin
if (MyThread = nil) then
MyThread := MsgThread.Create(false);
end;
procedure TForm1.BtnPostClick(Sender: TObject);
begin
if MyThread = nil then exit;
if PostThreadMessage(MyThread.ThreadID,
WM_MyMessage,0,0) then
Caption := ‘Post Message Ok!‘
else
Caption := ‘Post Message Fail!‘;
end;
procedure TForm1.BtnExitClick(Sender: TObject);
begin
MyThread.Free;
Close;
end;
end.
///////////// MyThread.pas/////////////////
unit MyThread;
interface
uses
Classes,windows, Messages;
const
WM_MyMessage=WM_USER 100;
type
MsgThread = class(TThread)
private
{ Private declarations }
FMyString : string;
protected
procedure Execute; override;
procedure ShowString;
end;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure PMessage.UpdateCaption;
begin
Form1.Caption := ‘Updated in a thread‘;
end; }
{ PMessage }
uses Main;
procedure MsgThread.Execute;
var Msg : TMsg;
begin
{ Place thread code here }
FMyString := ‘Thread Start!‘;
Synchronize(ShowString);
while (not Terminated) do
begin
if PeekMessage(Msg,0,0,0,PM_REMOVE) then
begin
if (Msg.message = WM_QUIT) then
begin
FMyString := ‘Thread Quit‘;
Synchronize(ShowString);
Terminate;
end;
if (Msg.message = WM_MyMessage) then
begin
FMyString := ‘Thread Get a USER Message!‘;
Synchronize(ShowString);
end;
end;
end;
end;
procedure MsgThread.ShowString;
begin
Form1.ListBox1.Items.Add(FMyString);
end;
end.
最好有实例!
// 用 PeekMessage 接收
// 一个例子
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,MyThread;
const
WM_MyMessage=WM_USER 100;
type
TForm1 = class(TForm)
BtnQuitThread: TButton;
ListBox1: TListBox;
BtnPost: TButton;
BtnStart: TButton;
BtnExit: TButton;
procedure BtnQuitThreadClick(Sender: TObject);
procedure BtnStartClick(Sender: TObject);
procedure BtnPostClick(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyThread : MsgThread;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.BtnQuitThreadClick(Sender: TObject);
begin
// MyThread.Terminate;
if MyThread = nil then exit;
if PostThreadMessage(MyThread.ThreadID,
WM_QUIT,0,0) then
Caption := ‘Post Message Ok!‘
else
Caption := ‘Post Message Fail!‘;
end;
procedure TForm1.BtnStartClick(Sender: TObject);
begin
if (MyThread = nil) then
MyThread := MsgThread.Create(false);
end;
procedure TForm1.BtnPostClick(Sender: TObject);
begin
if MyThread = nil then exit;
if PostThreadMessage(MyThread.ThreadID,
WM_MyMessage,0,0) then
Caption := ‘Post Message Ok!‘
else
Caption := ‘Post Message Fail!‘;
end;
procedure TForm1.BtnExitClick(Sender: TObject);
begin
MyThread.Free;
Close;
end;
end.
///////////// MyThread.pas/////////////////
unit MyThread;
interface
uses
Classes,windows, Messages;
const
WM_MyMessage=WM_USER 100;
type
MsgThread = class(TThread)
private
{ Private declarations }
FMyString : string;
protected
procedure Execute; override;
procedure ShowString;
end;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure PMessage.UpdateCaption;
begin
Form1.Caption := ‘Updated in a thread‘;
end; }
{ PMessage }
uses Main;
procedure MsgThread.Execute;
var Msg : TMsg;
begin
{ Place thread code here }
FMyString := ‘Thread Start!‘;
Synchronize(ShowString);
while (not Terminated) do
begin
if PeekMessage(Msg,0,0,0,PM_REMOVE) then
begin
if (Msg.message = WM_QUIT) then
begin
FMyString := ‘Thread Quit‘;
Synchronize(ShowString);
Terminate;
end;
if (Msg.message = WM_MyMessage) then
begin
FMyString := ‘Thread Get a USER Message!‘;
Synchronize(ShowString);
end;
end;
end;
end;
procedure MsgThread.ShowString;
begin
Form1.ListBox1.Items.Add(FMyString);
end;
end.
在线程中如何接收通过PostThreadMessage()发送的消息? - 程序开发常见问...
在OutlookExpress中如何设置163邮箱的接收和发送服务器?
在消息栏发送flash的通用代码
在消息栏发送flash的通用代码
帧的发送和接收过程
在基于对话框程序中使用WM_KICKIDLE消息 - ruo_gu的专栏 - CSDN博客
PB中使用AM消息集成组件进行消息发送
C#发送Email的3种途径实例讲解-程序开发-红黑联盟
在java中创建和运行简单的线程
发送wm_dropfiles消息
如何在消息框中显示一个超链接?
如何在消息框中显示一个超链接?
"本地连接"有发送无接收
如何在 Windows XP 中向“发送到”菜单添加菜单项
在Linux中通过三方软件添加和删除程序
在Linux中通过三方软件添加和删除程序-linux-Linux-天极Yesky
NS2脚本中如何使用God - 程序开发 - 文档专区 - Linux论坛
用C++写一发送邮件的程序
Java中的线程续 - 蓝色梦想的程序人生 - CSDNBlog
Java中的线程续 - 蓝色梦想的程序人生 - CSDNBlog
在autoconf/automake项目中禁止优化 - 程序开发 - 文档专区 - Linux论坛
一次可以发送接收10G文件的工具速度快、还有2G网络硬盘
MFC程序的消息处理顺序 (zz)
android调试程序,如何实现像java一样在控制台中输出结果_Android_手机开发...