Lazarus

Programming => LCL => Topic started by: Edson on December 15, 2020, 07:36:57 pm

Title: [Solved] MessageBox fails in Initialization
Post by: Edson on December 15, 2020, 07:36:57 pm
Hi.

Recently I was testing a program in a PC different from my system with not success.

Investigating, I arrive to this sample code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  6. type
  7.   TForm1 = class(TForm)
  8.   end;
  9.  
  10. var
  11.   Form1: TForm1;
  12.  
  13. implementation
  14. {$R *.lfm}
  15.  
  16. initialization
  17.   Application.MessageBox(PChar('Hello'), PChar('Hello'));
  18.  
  19. end.
  20.  

It fails. The message box never appears.

It was compilated in a Intel Core i7, x64 Windows 10 Enterprise 2015, using Lazarus 2.0.6 - FPC 3.0.4

Title: Re: MessageBox fails in Initialization
Post by: winni on December 15, 2020, 08:09:11 pm
Hi!

The initialization section is no place for GUI user interaction.
Not even the Form.create is a place for that.

The first moment when everything is initialized an correct shown is Form.activate

Astonishing enough: sometime your code works.

Winni
Title: Re: MessageBox fails in Initialization
Post by: lucamar on December 15, 2020, 09:13:52 pm
Explanation: Unit initialization blocks are executed before the main program block, where the Application object is itself initialized.

The Application object itself exists: it's created in the initialization of the Forms unit (otherwise you'd get a nice exception box :)) but it's not yet initialized so while MessageBox() might work sometimes it won't normally do it, because it's "parent" (let's call it that) isn't fully set up yet.

Contrary to what winni says, you can move that code to the form's OnCreate handler and the message box will show up before the form does, because by that time Application is fully operative. Or you can wait to OnActivate and the message box will show up over the brand new form. ;)

HTH!
Title: Re: MessageBox fails in Initialization
Post by: winni on December 15, 2020, 09:25:50 pm
@lucamar

You can put the MessageBox in FormCreate - BUT

In the most cases it shows an empty MessageBox -  the string is missing.
I am talking about Linu64/gtk2

I don't know with other OS.

Winni
Title: Re: MessageBox fails in Initialization
Post by: lucamar on December 15, 2020, 09:37:37 pm
@lucamar

You can put the MessageBox in FormCreate - BUT

In the most cases it shows an empty MessageBox -  the string is missing.
I am talking about Linu64/gtk2

I don't know with other OS.

Winni

I am on Linux 64 bit and it never fails (not that I use it that much, though). There is no reason for it to fail: at that point the only thing missing is to end creating the form(s) and calling Application.Run but that shouldn't interfere with showing a message box. Indeed, it's the almost the only place to show one if something went wrong when initializing the form objects.

You should be able to use MessageBox() any time after Application.Initialize; whether it has created any form or not. In fact, this works alright too:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, unit1
  11.   { you can add units after this };
  12.  
  13. {$R *.res}
  14.  
  15. begin
  16.   RequireDerivedFormResource := True;
  17.   Application.Scaled := True;
  18.   Application.Initialize;
  19.   Application.MessageBox('Ready to start rolling!!!', 'Very important!');
  20.   Application.CreateForm(TForm1, Form1);
  21.   Application.Run;
  22. end.

At least it does here (Xubuntu 18.04, Laz/FPC 2.0.10/3.2.0) - AMD64
Title: Re: MessageBox fails in Initialization
Post by: MarkMLl on December 15, 2020, 10:03:06 pm
You should be able to use MessageBox() any time after Application.Initialize; whether it has created any form or not.

Agreed, I use that fairly regularly to respond to e.g. an --about on the command line with a dialog(ue) box.

MarkMLl
Title: Re: MessageBox fails in Initialization
Post by: Edson on December 16, 2020, 03:56:44 am
Thanks all for the information. It's clear for me why the code it's failing.
TinyPortal © 2005-2018