加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

delphi Synchronize  过程

(2013-05-27 16:02:37)
分类: delphi
概述:
       VCL实现同步的另一种方法就是调用线程类的Synchronize的过程,此过程需要一个无参数的procedure,故在此procedure中无法传递参数值,但可以通过类的成员来实现。在类的Execute中只须调用Synchronize就可以了。
实现:
       关键在于对Synchronize参数的定义。定义一个无参数的procedure通过它来访问类的成员变量szName和nIndex。在类的重载Execute中调用Synchronize。
子类的定义如下:
unit TChildThread;
 
interface
 
uses
 Classes,Messages,Windows,SysUtils;
 
const MAX_LEN = 260;
type
 TChildThreads = class(TThread)
 private
    { Private declarations }
 protected
procedure Execute; override;
//同步函数的声明
    procedure UpdateData;
 public
     szName : array[0..MAX_LEN] of Char;
    nIndex : Integer;
 end;
 
implementation
uses
       Unit1;
 
{ Important: Methods and properties of objects in VCL or CLX can only be used
 in 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.
主程的设计与《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;


Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=726766

===============================================

另一个例子: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;   
    
    
   end.  



//////////////////////有是一篇文章//////////////

 

  当一个线程在使用时,如果这个函数使用了Synchronize修钸的话就不允许别一个线程来调用这个函数,它的目的是避免多个子线程同时访问主线程资源。

示例:

procedure ServerThread.ListItemEnd; //子线程的一个方法

begin

if (ListItem <> nil) then

ListItem.SubItems.Strings[3] := '传送完毕'; //ListItem是主线程的一个TreeView的一行

end;

procedure ServerThread.ClientExecute; //子线程执行

begin

Synchronize(ListItemEnd);

//如果直接写ListItemEnd,可能发生冲突。

end;

      当创建了多个线程,并且多个线程都要访问同一资源,,就有可能出现混乱,于是用Synchronize来控制,使同一时间只有一个线程使用那部分资源,Synchronize参数里面的代码就是多线程需要公共的代码!


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jqandjq/archive/2010/06/01/5638781.aspx

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有