Ben
Ben

Reputation: 3440

Messagebox delay in Delphi

I'm having problems with the messagebox API. I use messageboxw to ask a question to the user. For example my program is very busy with threads, etc. and when a user clicks on a button that shows the messagebox it doesn't show the messagebox until my program is less busy than before. When I remove the messagebox the code after it gets executed fine. I have too much code to show but maybe there is something that I have to take care of...

Anybody had this experience too?

Thanks for your help.

Upvotes: 2

Views: 754

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

What happens when you click the button is that a message is posted to the message queue. That doesn't get processed until you next pump your message queue. So, if you see a delay between clicking the button, and the program responding, that's because the message queue is not serviced in a timely fashion.

If your GUI thread is busy then the message queue won't be pumped until the main thread has finished whatever it is doing. And your GUI thread would be busy if you have long running tasks on it. Once the queued button click message eventually gets processed, then the call to MessageBoxW will result in the dialog showing immediately.

The only other reason why the GUI thread would not run would be if the CPU was consumed by higher priority threads. But that's pretty unlikely. It's very unusual for applications to use high priority threads. I'd be surprised if you were doing that.

How to solve the problem? If you have long running tasks on your main thread, move those tasks onto background threads. Or, if you have high priority threads that stop the GUI thread running, then run your background threads at normal priority.

Upvotes: 10

Related Questions