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. 如何在主線程停止/暫停線程/ Activity在android中暫停/停止?
- 4. 線程暫停
- 5. 暫停/恢復另一個線程
- 6. 請問socket.listen(0);暫停一個線程?
- 7. 暫停一個線程以防止ConcurrentModificationException?
- 8. 獲取一個線程暫停 - Thread.wait()/ Thread.notify()
- 9. 多線程暫停第二個線程
- 10. 如果一個線程正在console.readline上等待線程暫停?
- 11. 如何在一個方法中處理兩個線程暫停
- 12. pthread - 暫停/暫停所有線程
- 13. 暫停和取消暫停線程
- 14. 主線程暫停
- 15. Ruby暫停線程
- 16. 暫停和停止線程
- 17. 如何銷燬一個線程,暫停/掛起一個線程,恢復/運行一個線程?
- 18. 暫停/執行多線程程序中的一個線程(C#)
- 19. 如何在java中暫停一個線程?
- 20. Android - 如何在另一個活動期間暫停線程?
- 21. .net調試:如何暫停只有一個線程?
- 22. 如何暫停/掛起一個線程然後繼續呢?
- 23. 如何在javascript中暫停UI線程
- 24. Swift iOS如何暫停線程
- 25. 如何暫停主u.i線程(Android)
- 26. 如何暫停執行線程
- 27. 如何暫停和恢復線程
- 28. PyCharm - 如何暫停所有線程
- 29. 如何在java中暫停線程?
- 30. 如何暫停和恢復surfaceView線程
您設置了一個您的線程定期檢查的標誌。然後根據標誌的值繪製或不繪製。 –
使用事件和'WaitForSingleObject' ... – whosrdaddy
@DavidHeffernan - 好的。當標誌是'DoNotDraw'時,我用什麼來暫停線程? THread.Sleep(100)是一個好主意? – Ampere