[Solved] MessageBox fails in Initialization lazarus / 31-LCL ---------------------------------------------------------------------- Edson · 2020-12-15 18:36 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1;{$mode objfpc}{$H+}interfaceuses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs;type  TForm1 = class(TForm)  end; var  Form1: TForm1; implementation{$R *.lfm} initialization  Application.MessageBox(PChar('Hello'), PChar('Hello')); end.  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 ---------------------------------------------------------------------- winni · 2020-12-15 19:09 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 ---------------------------------------------------------------------- lucamar · 2020-12-15 20:13 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! ---------------------------------------------------------------------- winni · 2020-12-15 20:25 @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 ---------------------------------------------------------------------- lucamar · 2020-12-15 20:37 Quote from: 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 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Interfaces, // this includes the LCL widgetset  Forms, unit1  { you can add units after this }; {$R *.res} begin  RequireDerivedFormResource := True;  Application.Scaled := True;  Application.Initialize;  Application.MessageBox('Ready to start rolling!!!', 'Very important!');  Application.CreateForm(TForm1, Form1);  Application.Run;end. At least it does here (Xubuntu 18.04, Laz/FPC 2.0.10/3.2.0) - AMD64 ---------------------------------------------------------------------- MarkMLl · 2020-12-15 21:03 Quote from: lucamar on December 15, 2020, 09:37:37 pmYou 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 ---------------------------------------------------------------------- Edson · 2020-12-16 02:56 Thanks all for the information. It's clear for me why the code it's failing.