2019-06-23 Delphi多線程1

  首先當(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ò)誤,必須可視。


--------------------------------------------------------------------------------------------------------------------------

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容