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