Delphi线程同步(SendMessage)
来源: 阅读:1240 次 日期:2015-08-27 16:35:20
温馨提示: 小编为您整理了“Delphi线程同步(SendMessage)”,方便广大网友查阅!

Delphi线程同种的方法有很多种,除了常用的Synchronize方法,也可以使用SendMessage向主窗口发送消息,因为 SendMessage是阻塞的,可以达到同步的效果。主线程可以直接定义消息类型的procedure接收消息,也可以重写TControl类的 WndProc窗口过程,在窗口过程里面截取自己想要的消息。

Thread

{*******************************************************}

{ }

{ Delphi Thread Sample 5 }

{ Creation Date 2012.12.30 }

{ Created By: ming }

{ }

{*******************************************************}

unit unitWorkThread;

interface

uses

Classes,Windows, Messages, SysUtils, Graphics, StdCtrls;

type

TWorkThread = class(TThread)

private

{ Private declarations }

FEvent: HWND;

FMsg: string;

FMemo: TMemo;

FInterval,FTickTimes,FCount: Cardinal;

procedure doSyncProc1;

procedure syncOutputMsg;

procedure addLog(const msg: string); overload;

procedure addLog(const fmtStr:string; const params: array of const); overload;

procedure _sleep(millisecond:Cardinal);

protected

procedure Execute; override;

public

constructor Create(Suspend: boolean); overload;

constructor Create(Suspend: boolean; mmoOutput: TMemo); overload;

destructor Destroy; override;

private

FThreadPause,FThreadStop: Boolean;

procedure doSomething;

public

function ThreadStart: Boolean;

function ThreadPause: Boolean;

function ThreadStop: Boolean;

procedure ThreadTerminate;

public

MainFromHandle: HWND;

DoUpdateUI: procedure(const value: string) of object;

property Interval:Cardinal read FInterval write FInterval;

end;

const {0x0400 - 0x7FFF}

WM_UPDATE_UI1 = WM_USER + $1001;

WM_UPDATE_UI2 = WM_USER + $1002;

WM_UPDATE_UI3 = WM_USER + $1003;

var

WorkThread: TWorkThread;

implementation

{ TWorkThread }

constructor TWorkThread.Create(Suspend: boolean);

begin

inherited Create(Suspend);

FEvent := CreateEvent(nil,True,False,nil);

FreeOnTerminate := True;

FInterval := 1000;

end;

procedure TWorkThread.addLog(const msg: string);

begin

FMsg := msg;

Synchronize(syncOutputMsg);

end;

procedure TWorkThread.addLog(const fmtStr: string;

const params: array of const);

begin

FMsg := Format(fmtStr,params);

Synchronize(syncOutputMsg);

end;

constructor TWorkThread.Create(Suspend: boolean; mmoOutput: TMemo);

begin

inherited Create(Suspend);

FEvent := CreateEvent(nil,True,False,nil);

FreeOnTerminate := True;

FInterval := 1000;

FMemo := mmoOutput;

end;

destructor TWorkThread.Destroy;

begin

CloseHandle(FEvent);

inherited;

end;

procedure TWorkThread.doSomething;

begin

//addLog(FormatDateTime('c',now));

Inc(FCount);

FCount := FCount mod 100000;

SendMessage(MainFromHandle,WM_UPDATE_UI1,0,FCount);

SendMessage(MainFromHandle,WM_UPDATE_UI2,0,FCount);

SendMessage(MainFromHandle,WM_UPDATE_UI3,0,FCount);

// doSyncProc1;

end;

procedure TWorkThread.doSyncProc1;

begin

DoUpdateUI(IntToStr(FCount));

end;

procedure TWorkThread.syncOutputMsg;

var

dt: string;

begin

dt := FormatDateTime('hh:nn:ss',now);

FMsg := Format('[%s] - ',[dt]) + FMsg;

if Assigned(FMemo) then

FMemo.Lines.Add(FMsg);

end;

procedure TWorkThread.Execute;

begin

inherited;

while not Terminated do

begin

if WaitForSingleObject(FEvent,100)=WAIT_OBJECT_0 then

begin

Break;

end;

if (GetTickCount - FTickTimes) >= FInterval then

try

if not FThreadStop then

begin

doSomething;

FTickTimes := GetTickCount;

end;

except on e:Exception do

addLog(e.Message);

end;

if FThreadStop then

Suspend;

end;

end;

function TWorkThread.ThreadStart: Boolean;

begin

FThreadStop := False;

if Suspended then Resume;

end;

function TWorkThread.ThreadPause: Boolean;

begin

FThreadPause := True;

if not Suspended then Suspend;

end;

function TWorkThread.ThreadStop: Boolean;

begin

FThreadPause := False;

FThreadStop := True;

if Suspended then Resume;

end;

procedure TWorkThread.ThreadTerminate;

begin

FThreadStop := False;

if FEvent>0 then

begin

SetEvent(FEvent);

if Suspended then Resume;

end;

end;

procedure TWorkThread._sleep(millisecond: Cardinal);

begin

//WaitForSingleObject(Self.Handle,millisecond);

WaitForSingleObject(FEvent,millisecond);

end;

end.

Main form

unit main;

interface

uses

Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,

Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, unitWorkThread;

type

TfrmMain = class(TForm)

Memo1: TMemo;

btnStart: TButton;

btnPause: TButton;

btnStop: TButton;

Edit1: TEdit;

Edit2: TEdit;

Edit3: TEdit;

procedure FormCreate(Sender: TObject);

procedure FormDestroy(Sender: TObject);

procedure btnStartClick(Sender: TObject);

procedure btnPauseClick(Sender: TObject);

procedure btnStopClick(Sender: TObject);

private

{ Private declarations }

procedure UpdateUI(const value: string);

protected

procedure On_WM_UPDATE_UI(var msg: TMessage);message WM_UPDATE_UI1;

procedure WndProc(var Message: TMessage); override;

public

{ Public declarations }

end;

var

frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnPauseClick(Sender: TObject);

begin

WorkThread.ThreadPause;

end;

procedure TfrmMain.btnStartClick(Sender: TObject);

begin

WorkThread.ThreadStart;

end;

procedure TfrmMain.btnStopClick(Sender: TObject);

begin

WorkThread.ThreadStop;

end;

procedure TfrmMain.FormCreate(Sender: TObject);

begin

WorkThread := TWorkThread.Create(True,Memo1);

WorkThread.MainFromHandle := Self.Handle;

WorkThread.DoUpdateUI := UpdateUI;

end;

procedure TfrmMain.FormDestroy(Sender: TObject);

begin

WorkThread.ThreadTerminate;

end;

procedure TfrmMain.On_WM_UPDATE_UI(var msg: TMessage);

begin

Edit1.Text := IntToStr(msg.lParam);

end;

procedure TfrmMain.UpdateUI(const value: string);

begin

Edit1.Text := value;

end;

procedure TfrmMain.WndProc(var Message: TMessage);

begin

case Message.Msg of

WM_UPDATE_UI2:

begin

Edit2.Text := IntToStr(Message.lParam);

end;

WM_UPDATE_UI3:

begin

Edit3.Text := IntToStr(Message.lParam);

end;

else

inherited;

end;

end;

end.

更多信息请查看IT技术专栏

更多信息请查看网络编程
上一篇:delphi进程监测
手机网站地址:Delphi线程同步(SendMessage)
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!

2025国考·省考课程试听报名

  • 报班类型
  • 姓名
  • 手机号
  • 验证码
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map