前话: 其实你们要学会看源码, 我接下来要说的这些东东,与其等别人讲,还不如本身搞几个代码试一下,印象还深入点
TThread.Queue和TThread.Synchronize的区别,html
效果上:两者的做用都是让业务代码在主线程中执行,差异: Synchronize是阻塞,Queue是非阻塞xcode
代码上 两个方法最终都是调用的 class procedure TThread.Synchronize(ASyncRec: PSynchronizeRecord; QueueEvent: Boolean = False)类方法,多线程
差异ide
Synchronize则是使用了Thread对象中的FSynchronize对象变量,而后QueueEvent为False来调用TThread.Synchronize类方法, 函数
内部在执行FSynchronize时,建立了事件对象,经过WaitForSingleObject来阻塞执行。 .net
Queue调用是本身建立了一个PSynchronizeRecord, 而后QueueEvent为True来调用TThread.Synchronize类方法,内部则把PSynchronizeRecord放入SyncList列表中,而后退回,并不直接执行PSynchronizeRecord, 那问题来了,在那里执行呢?Delphi在TApplication.Idle方法中执行(最终调用了CheckSynchronize)线程
-----------------------------------------------------------------------------------------------------设计
Delphi中多线程用Synchronize实现VCL数据同步显示,Delphi中多线程用Synchronize实现VCL数据同步显示
转自:http://blog.csdn.net/maxcode/archive/2006/05/12/726766.aspxcode
概述:
VCL实现同步的另外一种方法就是调用线程类的Synchronize的过程,此过程须要一个无参数的procedure,故在此procedure中没法传递参数值,但能够经过类的成员来实现。在类的Execute中只须调用Synchronize就能够了。
实现:
关键在于对Synchronize参数的定义。定义一个无参数的procedure经过它来访问类的成员变量szName和nIndex。在类的重载Execute中调用Synchronize。子类的定义以下:
unit
TChildThread;
interfaceuses =Classes,
Messages,Windows,SysUtils;
const MAX_LEN = 260;
typeTChildThreads = class(TThread)
private { Private declarations }protectedprocedure Execute; override;//同步函数的声明
procedure UpdateData;
public
szName : array[0..MAX_LEN] of Char;
nIndex : Integer;end;implementationuses
Unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be usedin a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TChildThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TChildThread }//同步函数的实现procedure TChildThreads.UpdateData;begin Form1.ShowData.Items.Add(PChar(@szName));
end;
procedure TChildThreads.Execute;
begin{ Place thread code here }//调用同步过程
Synchronize(UpdateData);
end;
end.orm
主程的设计与《Delphi中多线程用消息实现VCL数据同步显示》基本一致,但为了与其显示相同结果,在生成子线程中语句顺序做了一下调整。如下代码仅显示与上一篇不一样的一个过程,其它代码再也不赘述。
procedure TForm1.StartThreadsClick(Sender: TObject);var
oChildThread : array[0..1000] of TChildThreads;
i : Integer;
begin
For i := 0 to 1000 do
begin
oChildThread[i] := TChildThreads.Create(true); //注意这里的代码与消息同步中的顺序。 oChildThread[i].nIndex := i;
strcopy(@oChildThread[i].szName,PChar('Child' + IntToStr(i)));
oChildThread[i].Resume;
end;
end;
===============================================
另外一个例子:http://topic.csdn.net/t/20011015/02/323001.html
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyThread = class(TThread)
private
child : TComponent;
public
procedure draw;
constructor Create(parent : TComponent);
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyThread }
constructor TMyThread.Create(parent: TComponent);
begin
child := parent;
inherited Create(false);
end;
procedure TMyThread.draw;
begin
if (child is TEdit) then
begin
(child as TEdit).Text := 'OK';
end
else if(child is TImage) then
begin
(child as TImage).Canvas.Brush.Color := clBlue;
(child as TImage).Canvas.FillRect(rect(0,0,100,100));
end;
end;
procedure TMyThread.Execute;
begin
inherited;
synchronize(draw);
if Terminated then Exit;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TMythread.Create(Edit1);
TMythread.Create(image1);
end;
http://blog.csdn.net/jiangnanandi/article/details/2962925