Trouble in GUI event handling lazarus / 31-LCL ---------------------------------------------------------------------- simsee · 2020-12-25 21:00 Suppose we have a GUI application with two forms (i.e. Form1, Form2), for which the OnShow event is handled as shown in the following code snippets: 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 procedure TForm1.FormShow(Sender: TObject);begin  writeln(1);  Form2.Show;  writeln(5);end; ... 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 procedure TForm2.FormShow(Sender: TObject);begin  writeln(2);  writeln(3);  writeln(4);end; The result obtained from the console is 1 2 3 4 5. This means that in TForm1.FormShow, the statement following Form2.Show (i.e. writeln (5)) is executed only after the conclusion of the TForm2.FormShow procedure. Is this sequence of operations guaranteed? Thanks in advance for the support. ---------------------------------------------------------------------- howardpc · 2020-12-25 21:33 In a single-threaded application statements are executed strictly in the logical  order in which they are coded. If this were not the case, no one could reliably write any  application, however simple or complex. ---------------------------------------------------------------------- lucamar · 2020-12-25 21:35 Quote from: simsee on December 25, 2020, 10:00:51 pmIs this sequence of operations guaranteed? Yes ... more or less. It has a very high likelihood of being always that way but it's not exactly guaranteed for all possible combinations of target OS/processor, though I can't think ATM of one where it would fail. That is, that it happens is system-dependent but most (all?) current systems behave that way. ---------------------------------------------------------------------- simsee · 2020-12-25 22:05 I forgot to say that the context is single threaded. My question is actually more general: in the hypothesis of single threaded application, is the instruction following the one that triggers an event executed only after the conclusion of the handling of that event? I believe that your answers also respond to my broader question. ---------------------------------------------------------------------- jamie · 2020-12-25 23:51 The SHOW method is a direct call to the code that would otherwise get called if and when it receives a WM_SHOWWINDOW or in Lazarus hood, LM_SHOWWINDOW  That is true for windows, on Linux however, I think they emulate the same process via a message que, for example a WM_ACTIVATE. That message is first sent to the window that is losing its attention and also has in the message the new window getting the attention and then it gets sent to the window being activated.  But these messages are on a posted que when done that way  If you were to use the ShowWindow API it would post these messages which would mean most likely your order would end differently  1,5, then 2,,3,4 because the messages aren't handled until you exit your code and have it return back to the message pump. ---------------------------------------------------------------------- simsee · 2020-12-26 10:46 A simple solution to guarantee the execution of the instructions in the expected sequence could be the use of a sort of semaphore. For the previous example: 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 procedure TForm1.FormShow(Sender: TObject);begin  writeln(1);  Form2.Show;  repeat until Form2.Shown;  writeln(5);end; 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   TForm2 = class(TForm)    procedure FormShow(Sender: TObject);  private   public    Shown : boolean;  end; 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 procedure TForm2.FormShow(Sender: TObject);begin  Shown:=False;  writeln(2);  writeln(3);  writeln(4);  Shown:=True;end; ---------------------------------------------------------------------- MarkMLl · 2020-12-26 11:25 Quote from: lucamar on December 25, 2020, 10:35:30 pmYes ... more or less. It has a very high likelihood of being always that way but it's not exactly guaranteed for all possible combinations of target OS/processor, though I can't think ATM of one where it would fail. That is, that it happens is system-dependent but most (all?) current systems behave that way. I think you could improve it by putting Flush(Output) after every Write() or WriteLn(); and Application.ProcessMessages after the Show. However I'd strongly advise against making any assumptions about your program being single-threaded, since you never know what bright ideas the OS or hardware developers will have in the future. MarkMLl