开发者

how to avoid "Not Responding" [duplicate]

This question already has answers here: Closed 12 years ago. 开发者_StackOverflow社区

Possible Duplicate:

Keep app responsive during long task

how to avoid "Not Responding", when processing large query or long process looping?

thx


Execute it in separate thread. (see TThread class)


Never ever use Application.ProcessMessages, it opens up a can of worms.
Really.
Let me repeat: DO NOT USE Application.ProcessMessages.
This is why: it requires all of your messaging to always be reentrant.
Your messaging, and more importantly messaging in libraries you use (the kind of which you cannot be sure it is reentrant).
Now, and in the future.
Even in cases you have not yet tested, or usage patterns you have not yet seen.

You should do multi-threading.
You really should.
It might take some time to get your synchronization right, but using somethine like a backgroundworker encapsulates most that in a neat way.

If you can't do multi-threading, then you can cheat.
But you shouldn't cheat.

Cheating is about postponing your real solution.
Postponing costs more than doing it now.

It is all about upstream decisions and downstream costs.
Fixing a bad decision later is way more expensive than taking the right decision now, and investing a bit of time to do it right.

Edit:

The only exception for using a secondary message loop is when showing modal forms or dialogs. There the secondary message loop has a confined reach of those messages because of the modality.

Edit 2:

The modality results in all other forms to be automatically disabled; timers and other non UI messages however are still being processed, so re-entrancy can still occur.

--jeroen


From my perspective you have two choices(depending on HOW LONG the task(or loop) will take):

  • use a thread as Alexander Malakhov mentioned;
  • add Application.ProcessMessages(forces the application to draw UI and to respond to events, but it's not as good when the loop takes a lot of time -- use thread..) after each loop completion if you would like the application to respond, however I would recommend it only for -small amount of time- loops, for long intensive loops use threads.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜