Open the Modal dialog box asynchronously in Delphi

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

In β€œModal” I just want to say that the user cannot interact with any other forms of the application until the modal dialog closes.

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

A modal form is one where the application cannot continue to work until the form is closed.

I currently have code like this:

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

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

How to show a form that prevents the user from interacting with the parent window without using ShowModal?

+6
delphi
source share
3 answers

Open the Delphi \ Source \ VCL \ Forms.pas source code and open the ShowModal implementation. Then find out how it works. I cannot copy the source code here, as it is IP CodeGear, but you can do it yourself and reuse parts of this code.

+6
source share

Even with the modal form open, the main thread is still executing (otherwise the modal form could not be redrawn).

Modal forms, however, have their own event loop, hindering the execution of the source application loop.

They should (like windows with Windows messages), because otherwise you could skip the event in the main event loop, creating another modal form or message.

And this type denies the whole essence of modality: in each thread of the user interface there can be only one modal form or message.

So you should ask yourself this question:

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

Then move these actions to a separate thread.

- Jeroen

+6
source share

Disable the parent form until your dialog box is visible, this will not allow users to interact with it. You can also use DisableTaskWindows to disable all forms, not just the parent form. It is not documented, but you can see how it is used in TCustomForm.ShowModal in 'forms.pas'.

+5
source share

All Articles