3
A
回答
6
您可能需要通過關鍵部分的fPaused/fEvent
保護。這取決於你的具體實現。
interface
uses
Classes, SyncObjs;
type
TMyThread = class(TThread)
private
fEvent: TEvent;
fPaused: Boolean;
procedure SetPaused(const Value: Boolean);
protected
procedure Execute; override;
public
constructor Create(const aPaused: Boolean = false);
destructor Destroy; override;
property Paused: Boolean read fPaused write SetPaused;
end;
implementation
constructor TMyThread.Create(const aPaused: Boolean = false);
begin
fPaused := aPaused;
fEvent := TEvent.Create(nil, true, not fPaused, '');
inherited Create(false);
end;
destructor TMyThread.Destroy;
begin
Terminate;
fEvent.SetEvent;
WaitFor;
fEvent.Free;
inherited;
end;
procedure TMyThread.Execute;
begin
while not Terminated do
begin
fEvent.WaitFor(INFINITE);
// todo: your drawings here
end;
end;
procedure TMyThread.SetPaused(const Value: Boolean);
begin
if (not Terminated) and (fPaused <> Value) then
begin
fPaused := Value;
if fPaused then
fEvent.ResetEvent else
fEvent.SetEvent;
end;
end;
+1
應該不是繼承構造函數的開始而是結束的時候? –
+1
這是在Delphi 6修復'TThread'構造函數推遲實際線程創建到'AfterConstruction'之前學習的一種常見模式。從那時起,沒有必要像上面那樣寫出來。 –
+0
@DavidHeffernan:請原諒我不知道,但自從D6以來,「以上」的內容沒有必要嗎? –
相關問題
- 1. 如何暫停一個線程(python)
- 2. 線程暫停
- 3. 暫停一個線程以防止ConcurrentModificationException?
- 4. 多線程暫停第二個線程
- 5. 如果一個線程正在console.readline上等待線程暫停?
- 6. 如何在一個方法中處理兩個線程暫停
- 7. 暫停和取消暫停線程
- 8. 如何銷燬一個線程,暫停/掛起一個線程,恢復/運行一個線程?
- 9. 如何在java中暫停一個線程?
- 10. Android - 如何在另一個活動期間暫停線程?
- 11. .net調試:如何暫停只有一個線程?
- 12. PyCharm - 如何暫停所有線程
- 13. 如何取消暫停線程的Qt
- 14. 如何暫停和恢復線程
- 15. 如何暫停執行線程
- 16. 如何暫停主u.i線程(Android)
- 17. 如何在java中暫停線程?
- 18. Swift iOS如何暫停線程
- 19. 如何暫停和恢復線程?
- 20. 如何暫停和恢復surfaceView線程
- 21. 如何暫停/恢復或停止一個線程和顯示彈出
- 22. 暫停和恢復一個C++線程在另一個
- 23. 殺死暫停的線程
- 24. 在ExitDialog上暫停線程
- 25. 什麼是暫停線程?
- 26. 暫停Windows服務線程?
- 27. Python暫停線程執行
- 28. 暫停/恢復線程?
- 29. 「暫停」,線程與屬性
- 30. 暫停在工作線程
您設置了一個您的線程定期檢查的標誌。然後根據標誌的值繪製或不繪製。 –
使用事件和'WaitForSingleObject' ... – whosrdaddy
@DavidHeffernan - 好的。當標誌是'DoNotDraw'時,我用什麼來暫停線程? THread.Sleep(100)是一個好主意? – Ampere