Delphi多線程編程中的技巧_第1頁
Delphi多線程編程中的技巧_第2頁
Delphi多線程編程中的技巧_第3頁
Delphi多線程編程中的技巧_第4頁
Delphi多線程編程中的技巧_第5頁
全文預覽已結束

Delphi多線程編程中的技巧.doc 免費下載

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

(1)創建線程

MsgThread:=TMsgThread.Create(False);//創建并執行線程

MsgThread:=TMsgThread.Create(True);//創建線程后掛起

constructorCreate(CreateSuspended:Boolean);中的參數CreateSuspended表示創建后是否掛起線程。

(2)設置線程里沒有設置循環執行的話,且設置FreeOnTerminate為True,則線程執行完后就會自己釋放。

(3)在一個線程結束后,調用另一個事件的方法:

只要設置Onterminate:=某方法,這樣在線程結束前自然會被調用,比如:

procedureTSendShortMessageThread.Execute;

var

Bitmap:Tbitamp;

begin

Bimap:=Tbitmap.create(nil);

OnTerminate:=Threaddone;

end;

procedureThreaddone(sender:tobject);

begin

Bimap.Free;//在Destory之前會被調用

end;

(4)程序結束前安全的退出線程的方法:

ifMsgThread<>nilthen

begin

MsgThread.Terminate;

MsgThread.WaitFor;

end;

(5)判斷當前線程的狀態:

//以下資料來自大富翁論壇。

/判斷線程是否釋放

//返回值:0-已釋放;1-正在運行;2-已終止但未釋放;

//3-未建立或不存在

functionTFrmMain.CheckThreadFreed(aThread:TThread):Byte;

var

i:DWord;

IsQuit:Boolean;

begin

ifAssigned(aThread)then

begin

IsQuit:=GetExitCodeThread(aThread.Handle,i);

ifIsQuitthen//Ifthefunctionsucceeds,thereturnvalueisnonzero.

//Ifthefunctionfails,thereturnvalueiszero.

begin

ifi=STILL_ACTIVEthen//Ifthespecifiedthreadhasnotterminated,

//theterminationstatusreturnedisSTILL_ACTIVE.

Result:=1

else

Result:=2;//aThread未Free,因為Tthread.Destroy中有執行語句

end

else

Result:=0;//可以用GetLastError取得錯誤代碼

end

else

Result:=3;

end;

(6)線程同步。

如果線程要調用VCL里面的內容(如:別的窗體中的控件),就需要將這個線程同步。線程同步表示交由主線程運行這段代碼,各個線程都在主線程中分時間段運行。另外,要想避免多個線程同時執行同一段代碼也需要將多線程同步。

臨界區和互斥的作用類似,都是用來進行同步的,但它們間有以下一點差別:

臨界區只能在進程內使用,也就是說只能是進程內的線程間的同步;而互斥則還可用在進程之間的;臨界區所花消的時間很少,才10~15個時間片,而互斥需要400多個;臨界區隨著進程的終止而終止,而互斥,如果你不用closehandle()的話,在進程終止后仍然在系統內存在,也就是說它是系統全局對象;

同步的方法有:

(1)使用臨界區對象。

臨界區對象有兩種:TRTLCriticalSection和CriticalSection。

TRTLCriticalSection的用法

var

GlobalVariable:Double;

var

CriticalSection:TRTLCriticalSection;

procedureSetGlobalVariable(Value:Double);

begin

EnterCriticalSection(CriticalSection);//進入臨界區

try

GlobalVariable:=Value;

finally

LeaveCriticalSection(CriticalSection);//離開臨界區

end;

end;

initialization

InitializeCriticalSection(CriticalSection);//初始化

finalization

DeleteCriticalSection(CriticalSection);//刪除

end.

CriticalSection(重要區段)的用法:

varcriticalsection:TCriticalsection;

創建:criticalsection:=TCriticalsection.create;

使用:

criticalsection.enter;

try

...

finally

criticalsection.leave;

end;

(2)使用互斥

先在主線程中創建事件對象:

var

hMutex:THandle=0;

...

hMutex:=CreateMutex(nil,False,nil);

在線程的Execute方法中加入以下代碼:

ifWaitForSingleObject(hMutex,INFINITE)=WAIT_OBJECT_0then

//DoSomething;

...

ReleaseMutex(hMutex);

最后記得要釋放互斥對象:

CloseHandle(hMutex);

(3)使用信號量

unitUnit1;

interface

uses

Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,

Dialogs,ExtCtrls,StdCtrls;

type

TForm1=class(TForm)

Edit1:TEdit;

Button1:TButton;

procedureFormCreate(Sender:TObject);

procedureFormDestroy(Sender:TObject);

procedureButton1Click(Sender:TObject);

private

{Privatedeclarations}

public

{Publicdeclarations}

end;

type

TMyThread=class(TThread)

private

protected

procedureExecute;override;

public

constructorCreate;virtual;

end;

var

Form1:TForm1;

HSem:THandle=0;

implementation

{$R*.dfm}

var

tick:Integer=0;

procedureTMyThread.Execute;

var

WaitReturn:DWord;

begin

WaitReturn:=WaitForSingleObject(HSem,INFINITE);

Form1.Edit1.Text:=IntToStr(tick);

Inc(tick);

Sleep(10);

ReleaseSemaphore(HSem,1,Nil)

end;

constructorTMyThread.Create;

begin

inheritedCreate(False);

FreeOnTerminate:=True;

end;

procedureTForm1.FormCreate(Sender:TObject);

begin

HSem:=CreateSemaphore(Nil,1,1,Nil);

end;

procedureTForm1.FormDestroy(Sender:TObject);

begin

CloseHandle(HSem);

end;

procedureTForm1.Button1Click(Sender:TObject);

var

index:Integer;

begin

forindex:=0to10do

begin

TMyTh

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論