小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

在線程中如何接收通過PostThreadMessage()發(fā)送的消息? - 程序開發(fā)常見問...

 imzjw 2007-06-22
在線程中如何接收通過PostThreadMessage()發(fā)送的消息?
  
最好有實(shí)例!

  
// 用 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.

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多