Which is the correct way to wait for a Thread.finalization and keep my application responsive

前端 未结 5 1320
陌清茗
陌清茗 2021-02-07 18:02

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
    App         


        
5条回答
  •  伪装坚强ぢ
    2021-02-07 19:04

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

    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.

    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.

提交回复
热议问题