Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a Modal dialog Asynchronously in Delphi

Tags:

delphi

Normally when you open a dialog using ShowModal, execution of the current thread halts until the dialog is closed. I want to display a Modal dialog, but continue execution on the current thread while the dialog is still open.

By "Modal" I simply mean that the user can not interact with any of the other forms of the application till the modal dialog is closed.

The Delphi ShowModal function provides a slightly different definition of "Modal" to the one I require:

A modal form is one where the application can't continue to run until the form is closed.

Currently I have code like this:

dialog.Parent:=self;
dialog.Show;
// keep doing stuff...

This works, except I can still interact with the parent window (move it around, close it etc)

How do I show a form that stops the user from interacting with the parent window, without using ShowModal?

like image 715
awmross Avatar asked Nov 22 '10 06:11

awmross


2 Answers

Open the source code of Delphi\Source\VCL\Forms.pas and open implementation of ShowModal. Then learn how it works. I can't copy the source code here as it's an IP of CodeGear, but you can do this yourself easily and reuse parts of it's code.

like image 100
Eugene Mayevski 'Callback Avatar answered Sep 29 '22 06:09

Eugene Mayevski 'Callback


Even with a modal form open, the main thread still executes (otherwise the modal form could not repaint itself).

Modal forms however have their own event loop, preventing the original application event loop from executing.

They have to (just like Windows message boxes have to as well), as otherwise you could have an event sneak back into the main event loop creating yet another modal form or messagebox.

And that kind of negates the whole point of being modal: there can be only one modal form or messagebox per UI thread.

So you need to ask yourself this question:

What actions in the main event loop does this modal form prevent from happening?

Then move those actions into a separate thread.

--jeroen

like image 45
Jeroen Wiert Pluimers Avatar answered Sep 29 '22 04:09

Jeroen Wiert Pluimers