TThread-簡單的開始
TMyThread = class(TThread) private FCount: Integer; FOnShowValue: TGetStrProc; protected procedure Execute; override; public constructor Create; property Count: Integer read FCount; property OnShowValue: TGetStrProc read FOnShowValue write FOnShowValue; end; { TMyThread } constructor TMyThread.Create; begin inherited Create(False); FCount := 0; FreeOnTerminate := False; end; procedure TMyThread.Execute; begin while not self.Terminated do begin Inc(FCount); if Assigned(FOnShowValue) then FOnShowValue(IntToStr(FCount)); Sleep(100); end; end;
代碼中只覆蓋了一個Execute方法即可,其他的代碼都是業務相關的代碼,還是非常簡單好用。
線程掛起
線程終止
在Delphi的TThread類實現中,可以通過一個Terminate方法來讓線程終止。但事實上Terminated只是一個標識而已,在線程啟動時這個標識為False。
一般線程創建后運行完會自動釋放,所以這里的類里我設置FreeOnTerminate := False;,這樣線程對象就不會自動釋放,這樣做的好處就是可以由線程對象以外的代碼來管理線程的生命周期,如果有的代碼需要控制線程對象的生命周期就可以用這個屬性來讓線程不自己釋放。
ThreadProc-源代碼分析
function ThreadProc(Thread: TThread): Integer; var FreeThread: Boolean; begin {$IFDEF LINUX} if Thread.FSuspended then sem_wait(Thread.FCreateSuspendedSem); {$ENDIF} try if not Thread.Terminated then try Thread.Execute; except Thread.FFatalException := AcquireExceptionObject; end; finally FreeThread := Thread.FFreeOnTerminate; Result := Thread.FReturnValue; Thread.FFinished := True; Thread.DoTerminate; if FreeThread then Thread.Free; {$IFDEF MSWINDOWS} EndThread(Result); {$ENDIF} {$IFDEF LINUX} // Directly call pthread_exit since EndThread will detach the thread causing // the pthread_join in TThread.WaitFor to fail. Also, make sure the EndThreadProc // is called just like EndThread would do. EndThreadProc should not return // and call pthread_exit itself. if Assigned(EndThreadProc) then EndThreadProc(Result); pthread_exit(Pointer(Result)); {$ENDIF} end; end;
對于TThread的一個關鍵部分就是這個ThreadProc方法,它是線程創建時傳給系統API的回調函數;Delphi中通過這個方法完成了一個核心的功能,可以看到代碼中調用了Execute方法。這也就是為什么派生類只要覆寫這個方法的原因。
所以從代碼也可以看出,線程啟動后代碼是順序執行的,代碼走完就結束了,所以為了讓線程能夠一直在運行就要在Execute方法里加上一個死循環,保證線程一直在運算,直到接收到Terminated時才讓線程結束掉。所以Terminated的真正作用在這呢,需要開發者自己來控制,當然這樣也就變的非常靈活了。
| 不含病毒。www.avast.com |
請先 登入 以發表留言。