Delphi中多執行緒中Synchronize的運用

2021-04-20 07:03:02 字數 2826 閱讀 2522

delphi中多執行緒用synchronize實現vcl資料同步顯示,delphi中多執行緒用synchronize實現vcl資料同步顯示

概述:        

vcl實現同步的另一種方法就是呼叫執行緒類的synchronize的過程,此過程需要乙個無引數的procedure,故在此procedure中無法傳遞引數值,但可以通過類的成員來實現。在類的execute中只須呼叫synchronize就可以了。

實現:      

關鍵在於對synchronize引數的定義。定義乙個無引數的procedure通過它來訪問類的成員變數szname和nindex。在類的過載execute中呼叫synchronize。子類的定義如下:

unit

tchildthread;

inte***ceuses =classes,

messages,windows,sysutils;

const max_len = 260;

typetchildthreads = class(tthread)

private      protectedprocedure execute; override;//同步函式的宣告

procedure updatedata;

public      

szname : array[0..max_len] of char;  

nindex : integer;end;implementationuses         

unit1;

//同步函式的實現procedure tchildthreads.updatedata;begin         form1.showdata.items.add(pchar(@szname));

end;

procedure tchildthreads.execute;

begin//呼叫同步過程

synchronize(updatedata);

end;

end.

另乙個例子:http://topic.csdn.net/t/20011015/02/323001.html

unit    unit1;  

inte***ce  

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  

public  

end;  

tmythread    =    class(tthread)  

private  

child    :    tcomponent;  

public  

procedure    draw;  

constructor    create(parent    :    tcomponent);  

procedure    execute;    override;  

end;  

var  

form1:    tform1;  

implementation  

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;

delphi 多執行緒

摘自 萬一的部落格 functionmyfun p pointer integer stdcall var i integer begin fori 0to500000do begin form1.canvas.lock form1.canvas.textout 10,10,inttostr i f...

delphi 多執行緒

看別人的部落格一萬次記憶效果也沒那麼好,還是自己動手寫寫吧!functioncreatethread lpthreadattributes pointer dwstacksize dword lpstartaddress tfnthreadstartroutine lpparameter point...

delphi 多執行緒例項

下面筆者將介紹乙個簡單的例項,解釋和說明前面所述的內容。1 建立form,在其中建立兩個按鈕 開始採集 和 停止採集 新增乙個paintbox1。2 建立乙個新的單元,在其中輸入以下 unit collectthread inte ce uses classes,sysutils,stdctrls,...