首先當(dāng)然是打開你的delphi 6 ,點(diǎn)取菜單欄中的文件-新建-其它,彈出一個(gè)標(biāo)簽窗口,選取new標(biāo)簽,然后找到Thread Object,就是它了,雙擊它就行了,彈出一個(gè)類命名窗口,輸入mythread,當(dāng)然名稱可由你自已來(lái)定的。這時(shí)程序自動(dòng)創(chuàng)建一個(gè)unit,我這里是unit2,現(xiàn)在我們來(lái)看unit,代碼如下:
unit Unit2;
interface
uses
Classes;
type
mythread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
{ 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 mythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;?}
{ mythread }
procedure mythread.Execute;
begin
{ Place thread code here }
end;
end.
其中,你注意找到procedure mythread.execute;,應(yīng)找到了吧,連我都看到了,這就是你剛才建立的線程了,那么接下來(lái),我們要做的就是加入后臺(tái)執(zhí)行的代碼,代碼要加在那里?不會(huì)吧,當(dāng)然是加在
begin
//這里就是加入程序代碼的地方了
end;
//implementation后面的uses 添加?
如果你要調(diào)用unit1上的控件,你可以在unit2上面的implementation后面的uses 添加?中加入? uses unit1; 就行了,記住,在unit1里的implementation后面增加uses unit2,這樣你就可在unit1中引用線程了,引用的方法很簡(jiǎn)單,就是,就是,就是,好啦,不賣關(guān)了,就是?
procedure TForm1.Button1Click(Sender: TObject);
begin
??mythread.Create(false);? ?
end;
OK?這就是delphi中的線程,呵呵。
多線程中MEMO 組件的窗體是不能隱藏不可見的,那樣會(huì)造成錯(cuò)誤,必須可視。
--------------------------------------------------------------------------------------------------------------------------