当前位置:学Delphi网文档资料专题讲解多线程

在多线程里使用Windows消息的例子

减小字体 增大字体 作者:本站  来源:本站原创  发布时间:2010-09-26 23:33:37

大家都知道很多VCL都是线程不安全的,本文主要内容是如何在多线程里使用windows消息来达到线程同步的目的,以下是实例的代码:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  WM_AddItem=WM_USER+1001;

type
  TTestThread = class(TThread)
  private
    FHandle:THandle;
    FOnAddItemEvent: TNotifyEvent;
    procedure SetOnAddItemEvent(const Value: TNotifyEvent);
    procedure DoAddItem;
  protected
    procedure Execute; override;
    procedure WndMethod(var Msg: TMessage);
  public
    constructor Create;
    destructor Destroy; override;
    property OnAddItemEvent:TNotifyEvent read FOnAddItemEvent write SetOnAddItemEvent;
  end;

  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FTestThread:TTestThread;
    procedure DoAddItemEvent(Sender: TObject);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TTestThread }

constructor TTestThread.Create;
begin
  inherited Create(True);
  FHandle := AllocateHWnd(WndMethod);
end;

destructor TTestThread.Destroy;
begin
  DeallocateHWnd(FHandle);
  inherited;
end;

[1] [2]  下一页

  • 好的评价 如果您觉得此文章好,就请您
      100%(1)
  • 差的评价 如果您觉得此文章差,就请您
      0%(0)

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论