1

The demo application below asks the user if they really want to close when they try to close the form.

This works fine when the form is not minimised. However, if they try to close the application from the Taskbar when the form is minimised, the app gets locked up and can only be closed from Task Manager.

It seems that the modal warning message is shown but not visible. Since they can't close the modal message box, they can't close it at all, even after restoring the window.

Note that a VCL application with similar code, using TApplication.MessageBox does not have an issue. The message box appears as soon as the X is clicked from the Taskbar and the app can be closed.

Note also that a similar FMX application made in Delphi 10 does not have the issue.

Does anyone have any ideas for a solution?

I've tried forcing the form's WindowState to wsNormal before showing the message box and calling Application.ProcessMessages, but it doesn't change anything.

I'm using a synchronous message box because this is to prevent loss of unsaved work in my application. I don't want the user to be able to close the application until they've clicked Yes in the message box.

DFM:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 347
  ClientWidth = 437
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnClose = FormClose
  DesignerMasterStyle = 0
end

Code:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs;

type
  TForm1 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
  FMX.DialogService.Sync;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if TDialogServiceSync.MessageDialog(
           'Are you sure you want to close?', TMsgDlgType.mtWarning,
           [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo],
           TMsgDlgBtn.mbYes, 0) = mrNo then
    Action := TCloseAction.caNone;
end;

end.
Ken White
  • 123,280
  • 14
  • 225
  • 444
XylemFlow
  • 963
  • 5
  • 12
  • 1
    This probably won't solve the issue, but you should be using the `OnCloseQuery` event for this prompting, instead of using the `OnClose` event. – Remy Lebeau May 07 '23 at 18:11
  • @RemyLebeau OK thanks. Same issue though. Note that a VCL application with similar code, using TApplication.MessageBox does not have the same issue. – XylemFlow May 08 '23 at 12:40
  • I've just noticed that there are issues closing the application from the Taskbar even without the message dialog. When minimised the application won't close immediately. It will only close after restoring it. BTW, I'm talking about closing by hovering the cursor over the Taskbar icon and clicking the X in the top right corner of the preview. – XylemFlow May 08 '23 at 13:38
  • An application made in delphi 10 does not have the issue either. Maybe I should just report this as a bug. – XylemFlow May 08 '23 at 13:44
  • 1
    That is definitely a bug. As far as I see it always happens if you try to show any modal message when FMX application is in minimized state. First of all that modal message or is never shown. After that something gets really broken because now you cant even minimize your application normally. So I'm guessing that somehow this messes up the message loop. But I can't figure it out how. PS: I'm using Delphi 11 Version 28.0.46141.0937 – SilverWarior May 08 '23 at 19:59
  • I've reported this in RSP-41651 and it has been confirmed as a defect and entered into the internal bug tracking system. Hopefully it will be fixed. – XylemFlow May 09 '23 at 10:15
  • While trying to find a workaround, I found another issue. The reason I can't force the form to restore before showing the message box is because the form's WindowState is wsNormal, even though the form is minimized. Any idea why that would be? How can I detect if the form is minimized? – XylemFlow May 10 '23 at 15:56

1 Answers1

0

This is a bug in Delphi 11, which is now under investigation. However, the workaround is fairly simply. Just Show the form before the message box.

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Show; // Make sure the form isn't minimized

  if TDialogServiceSync.MessageDialog(
           'Are you sure you want to close?', TMsgDlgType.mtWarning,
           [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo],
           TMsgDlgBtn.mbYes, 0) = mrNo then
    CanClose := False;
end;
XylemFlow
  • 963
  • 5
  • 12