在TThread派生类中重新创建TThread [英] Recreating a TThread Inside a TThread dervied class

查看:89
本文介绍了在TThread派生类中重新创建TThread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个从TThread类派生的新类,并在构造函数上调用继承的Create(True);",然后调用"Resume()",因为我已经覆盖了Execute()调用,现在我想回想一下Execute()(再次运行线程)而不会破坏类实例,因此在新类内部有一个名为"myRestart()"的函数,该函数可调用继承的Create(True);".并使我能够再次调用"Resume()",然后线程再次工作.

I created a new class derived from TThread class, and on the constructor i call "inherited Create(True);", and then call "Resume()" since i have override the Execute() call, now i wanna recall the Execute() (Run the Thread Again) without destroying the class instance, so i have a function inside the new class called "myRestart()", which recalls "inherited Create(True);" and makes me able to call "Resume()" again and thread works again.

我的问题是,这是安全的做法吗?如果我有该类的多个实例,它也会工作吗?还是有更好的方法呢?

my question is, is this a safe practice? will it work also if i have multiple instances of this class? or is there a better way to do it?

谢谢

推荐答案

不要四处走动.如果您希望线程类中的过程/函数多次运行,请从Execute覆盖中的while()循环中调用它们,并用最合适的同步对象,信号灯或事件向线程发出信号以运行代码,说:

Don't go around doing things like that. If you want procedures/functions in your thread class to run more than once, call them from a while() loop in your Execute override and signal the thread to run the code with a suitable synchro object at the top, a semaphore or event, say:

TmyThread.Execute;
begin
  while true do
  begin
    someEvent.waitFor(INFINITE);
    if terminated then exit;
    doMyProcedure(params);
    doOtherStuff;
  end;
end;

这篇关于在TThread派生类中重新创建TThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆