多线程 – 如果主线程完成,我是否必须发出匿名线程的信号?
发布时间:2020-12-15 04:25:27 所属栏目:Java 来源:网络整理
导读:参见英文答案 Is it necessary to explicitly stop all threads prior to exiting a Win32 application?????????????????????????????????????5个 假设我有一个匿名线程进行一些后台处理: procedure TForm1.FormCreate(Sender: TObject);begin TThread.Crea
参见英文答案 >
Is it necessary to explicitly stop all threads prior to exiting a Win32 application?????????????????????????????????????5个
假设我有一个匿名线程进行一些后台处理: procedure TForm1.FormCreate(Sender: TObject); begin TThread.CreateAnonymousThread(procedure begin while True do begin Sleep(500); OutputDebugString('I am alive'); end; end).Start; end; 该线程不与主线程共享任何资源,它只是坐在那里并“永远”运行. 由于没有像Terminate for anonymous threads这样的内置机制,这是否意味着当进程的主线程退出时我不必通知线程? 如果您只是启动一个新的VCL应用程序,请在FormCreate事件中粘贴上面的代码,它将写入我每半秒钟调试消息.当应用程序退出时(即通过关闭表单),似乎该线程也退出,尽管它不检查任何信号. 没关系,还是我必须使用TEvent或类似的方法实现一些信号来通知线程? 或者编写一个自定义的TThread后代并将该线程的引用保留给Thread.Free以后会更好吗? 解决方法
您需要终止您创建的任何线程,甚至是匿名线程,例如:
procedure TForm1.FormCreate(Sender: TObject); begin TThread.CreateAnonymousThread(procedure begin while not Application.Terminated do begin // <-- Sleep(500); OutputDebugString('I am alive'); end; end).Start; end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |