让线条运行在表格上 [英] Let stay thread running on form close

查看:118
本文介绍了让线条运行在表格上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中做了一个同步线程,我想知道有没有办法让线程保持开放,直到它完成同步过程,如果我关闭应用程序表单?

解决方案

在Rob的优秀答案的评论中,提出了等待时显示UI的问题。如果您需要在等待时显示UI,那么您将需要一个更高级的等待, TThread.WaitFor 。这是我使用的,依靠 MsgWaitForMultipleObjects

  procedure WaitUntilSignaled(Handle:THandle; ProcessMessages:布尔); 
begin
如果ProcessMessages然后开始
Application.ProcessMessages; //如果有任何消息已经在队列中等待
,而MsgWaitForMultipleObjects(1,Handle,False,INFINITE, QS_ALLEVENTS)= WAIT_OBJECT_0 + 1 do begin
Application.ProcessMessages;
结束
end else begin
WaitForSingleObject(Handle,INFINITE);
结束
结束

....

Form.Show;
WaitUntilSignaled(Thread.Handle,True);
Form.Close;


I made a syncing thread on my application and I want to know is there a way to let the thread stay open until it finishes it's syncing process, if i close the application form?

解决方案

In the commentary to Rob's excellent answer, the question of showing UI whilst waiting was raised. If you need to show UI whilst waiting, then you will need a more advanced wait that TThread.WaitFor. This is what I use in its place, relying on MsgWaitForMultipleObjects.

procedure WaitUntilSignaled(Handle: THandle; ProcessMessages: Boolean);
begin
  if ProcessMessages then begin
    Application.ProcessMessages;//in case there are any messages are already waiting in the queue
    while MsgWaitForMultipleObjects(1, Handle, False, INFINITE, QS_ALLEVENTS)=WAIT_OBJECT_0+1 do begin
      Application.ProcessMessages;
    end;
  end else begin
    WaitForSingleObject(Handle, INFINITE);
  end;
end;

....

Form.Show;
WaitUntilSignaled(Thread.Handle, True);
Form.Close;

这篇关于让线条运行在表格上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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