这是等待Thread.finalization并使我的应用程序保持响应的正确方法 [英] Which is the correct way to wait for a Thread.finalization and keep my application responsive

查看:96
本文介绍了这是等待Thread.finalization并使我的应用程序保持响应的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在使用此代码并且工作正常,但是我想知道是否正确的方法.

Actually i am using this code and works ok, but i 'am wondering if is the correct way.

  while WaitForSingleObject(MyThread.Handle, 0) = WAIT_TIMEOUT do
    Application.ProcessMessages;

  ShowMessage('i am done');

推荐答案

调用Application.ProcessMessages通常被认为是代码异味.如果您无事可做,请让您的主线程空闲.

Calling Application.ProcessMessages is generally considered a code smell. Let your main thread idle if it's got nothing to do.

如果您经营一家公司并需要一名工人奔赴商店并抢走一些急需的用品,那么您会在门前pace步直到他回来,还是您想坐在办公室休息呢?等他,发现那里的物资在这里,因为你听见他走过门?无论哪种方式,他都将花费相同的时间,但是第一种方式将使您的双腿枯竭.

If you ran a company and needed one of your workers to run to the store and grab some much-needed supplies, would you then pace by the door until he got back, or would you prefer to sit in your office and rest and wait for him, and find out that the supplies are here because you hear him walk through the door? Either way, he'll take the same amount of time, but the first way's gonna wear your legs out.

类似地,不要让您的UI监视线程,而是让线程报告返回给UI.一种实现方法是让线程使用PostMessage将自定义消息发送到启动它的表单,并在表单完成后将消息处理程序放置在表单上以对其进行响应.

Similarly, instead of having your UI watch the thread, have the thread report back to the UI. One way to do this is to have the thread use PostMessage to send a custom message to the form that launched it once it's finished, and put a message handler on the form to respond to it.

这篇关于这是等待Thread.finalization并使我的应用程序保持响应的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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