5

I have an application that can run for quite a long time scanning a database.
During this process I keep my program responsive by using processmessage.
This processmessage is triggered when my progress bar is updated and inc'ed.
This works fine is most cases, but when the databases get larger it takes longer for the progress bar to jump up 1%, the program becomes unresponsive until that time.

Is there another way to keep my program alive besides processmessages?

6
  • 1
    Try using Application.HandleMessage instead. Also store the LastPercentage as a variable. Calculate the NewPercentage and exit if the NewPercentage = LastPercentage. Jan 8, 2013 at 13:22
  • @CapeCodGunny No such thing. You mean Application.HandleMessage? Why would that help? Jan 8, 2013 at 13:22
  • @David - Thanks I have edited my comment. It breaks in and forces just the one update not all pending Windows updates. Jan 8, 2013 at 13:27
  • @CapeCodGunny I know what it does. Why would it help? Jan 8, 2013 at 13:28
  • @DavidHeffernan - It won't help if the percentage takes a long time to increment. Jan 8, 2013 at 13:31

2 Answers 2

8

Multi threading is the answer. A standard Delphi application is basically a single threaded application that can do one thing at a time. Hence the gui lockup, it can't remain responsive if it's doing something else.

If you want to have a responsive gui and do heavy lifting at the same time, you need to have the heavy lifting in a separate thread or threads. This way your main thread can make sure you have a responsive program and the worker threads do the heavy lifting. This works nice for heavy database work but also for for instance the downloading of files or situations where an answer of for instance a remote server can take a long time.

But this answer will probably give you more questions then answers because to explain HOW to use multi threading would be too big of an explanation for this question.

One other thing though: have a long and hard look at your database code. How are you retrieving records from the database, are there good indexes on the database etc. etc. etc. You can get insane speed improvements by optimizing this code before you have to start thinking about multi threading.

I've found the following resource: http://thaddy.co.uk/threads/ which you can download with pictures at: http://cc.embarcadero.com/item/14809 to be very usefull threading tutorial.

3
  • 5
    A standard Delphi app is not single threaded. Having multiple threads doesn't solve the problem. Moving long running work off the GUI thread is what solves the problem. Jan 8, 2013 at 13:15
  • @DavidHeffernan now you have me confused. I was always under the impression that a standard Delphi program for all intends and purposes behaved as a single threaded program unless you specifically started extra threads. Having long running work off the GUI thread is exactly what my intention with the answer is.
    – Pieter B
    Jan 8, 2013 at 13:21
  • I mean that you should make that clear in the answer. You don't explicitly state that your advice is to put the DB work on a different thread. Jan 8, 2013 at 13:23
4

If you want to make your GUI program appear responsive, you must service the message queue in a timely fashion. There is no alternative.

When it comes to running database queries, the way to do that without freezing your UI, is to move the query to a different thread.

2
  • What's the strategy to update UI from a thread? I always run into this problem when the main program is exiting but the thread is still working. Jan 8, 2013 at 12:55
  • 1
    @LeonardoHerrera I don't see how those two concerns are related. For updating UI from a thread normally message passing (and not necessarily Windows messages) is the best approach. Jan 8, 2013 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.