[Solved] *Tip* request to shunt WndProc + Dispatch + msgHandler lazarus / 31-LCL ---------------------------------------------------------------------- devEric69 · 2021-10-28 20:36 Hello, I've got a TEdit with its sequence of events triggered, being: - OnKeyPress - OnChange If in the OnKeyPress event, I return a Chr(0) i.e. no change. In this case, I would to shunt \ escape the following OnChange event. So I've coded (in OnKeyPress, i.e. before OnChange)... 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 if Key = chr(0) then    SysUtils.Abort; ...which generates the exception EAbort: I should be happy. But, I still have the OnChange event that is fired after, triggered via this call stack: 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 function GtkEntryDelayCursorPos(AGtkWidget: Pointer): GBoolean; cdecl; ➔ function gtkchanged_editbox( widget: PGtkWidget; data: gPointer) : GBoolean; cdecl; ➔ DeliverMessage(const Target: TObject; var AMessage): PtrInt; ➔ TWinControl.WndProc(var Message: TLMessage); ➔ procedure TObject.Dispatch(var message); ➔ msgHandler(message); ➔ procedure TControl.CMTextChanged(var Message: TLMessage); ➔ DoEditForSearch_Change The Abort \ EAbort exception documentation says: QuoteAbort is raised by the Abort procedure. It is not displayed in GUI applications, and serves only to immediately abort the current procedure, and return control to the main program loop. My question is: is there a way (in the message handling layer API) to remove \ reset all the messages from the main program loop (here, SysUtils.Abort doesn't prevent the message CM_TEXTCHANGED - already posted \ sent, it seems, hence - from being peeked and processed by the main program loop despite and after a call to Abort)? Regards. ---------------------------------------------------------------------- howardpc · 2021-10-28 20:49 Would it not be simpler to do something like the following? 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.Edit1KeyPress(Sender: TObject; var Key: char);begin  if Key = Chr(0) then    begin      Edit1.OnChange := Nil;      Key := Chr(0);    Edit1.OnChange := @Edit1Change;    end;end; ---------------------------------------------------------------------- devEric69 · 2021-10-29 07:47 Quote from: howardpc on October 28, 2021, 10:49:38 pmWould it not be simpler to do something like the following? This is a trick that works very well. Thank you very much . Now, my question was theoretical \ technical. EAbort says it's done to abandon the current procedure (so, first of all, what is the "current" procedure?). Basically, I have this sequence of calls, that could be like this like one: 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 TEdit.keyPressedDetected; LM_Key_Pressedbegin    Self.DoObjectCaretBlinkingInMyself;    Self.Perform(LM_key_will_be_processed);    PostMessage(LM_objet_caret_must_no_longer_blink_in_me);    if Assigned(FOnKeyPressed) then FOnKeyPressed(Self);    Self.AOwner.DoThis;    Self.DoObjectCaretStopBlinkingInMyself;    SendMessage(LM_object_beep_please_answer_me_you_will_not_buzz);    if Assigned(FOnChanged) then FOnChanged(Self)    Self.DoObjectCaretBlinkingInMyself;end; Shouldn't EAbort make it possible to shunt everything \ completely clean the calls stack and the messages sent by this latter (i.e. calls via pointers, or via synchronous messages or via asynch. messages) that is \ are after FOnKeyPressed(Self)? I wonder because I don't see \ anderstand the difference - in my case  - between a call to EAbort and a call to Exit (but I don't know much about the Lazarus message processing loop ; I know, I imagine that to " glue " in the same loop of treatment, all the messages of many operating systems is not simple; so, I don't know if what I'm saying \ asking is "science fiction" , or a miss that could be done in a sense i.e. a possible "Todo" among many others already existing )... ---------------------------------------------------------------------- Remy Lebeau · 2021-10-30 18:28 Quote from: howardpc on October 28, 2021, 10:49:38 pmWould it not be simpler to do something like the following? Such code is completely useless.  First, because Key will never be #0 on entry, you return #0 to cancel further processing of the current key stroke.  And second, setting the Key is just a variable assignment, so it will not trigger a nested OnChange event, so setting the OnChange property just before assigning the Key and then resetting the property immediately afterwards has no effect at all. If you want to ignore the next OnChange event, you will have to flag that manually, eg: 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 var  SkipNextChangeEvent: Boolean = False; procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);begin  if Key = ... then  begin    SkipNextChangeEvent := True;    ...  end;end; procedure TForm1.Edit1Change(Sender: TObject);begin  if SkipNextChangeEvent then  begin    SkipNextChangeEvent := False;    Exit;  end;  ...end; Quote from: devEric69 on October 29, 2021, 09:47:36 amNow, my question was theoretical \ technical. EAbort says it's done to abandon the current procedure (so, first of all, what is the "current" procedure?). The procedure that is calling Abort(). Quote from: devEric69 on October 29, 2021, 09:47:36 amBasically, I have this sequence of calls, that could be like this like one: ... Shouldn't EAbort make it possible to shunt everything \ completely clean the calls stack and the messages sent by this latter (i.e. calls via pointers, or via synchronous messages or via asynch. messages) that is \ are after FOnKeyPressed(Self)? Abort() simply raises an EAbort exception, nothing more.  And like any raised exception, any further procedure calls in the raising thread are bypassed until the exception is caught, or it escapes the thread, terminating it.  In the case of EAbort, the main message loop will catch it so it doesn't terminate the app completely, but the currently running message handler will certainly be ended, yes.  But any pending window messages will still be pending, and will be processed at a later time.  The only way to skip those messages is to manually retrieve them from the message queue and discard them without dispatching them, using a PeekMessage(PM_REMOVE) loop (which I don't advise you do, unless you REALLY know what you are doing). Quote from: devEric69 on October 29, 2021, 09:47:36 amI wonder because I don't see \ anderstand the difference - in my case  - between a call to EAbort and a call to Exit Exit merely exits the procedure that is calling it, returning flow to the caller so it can continue on with its work. Abort() raises an exception, jumping flow to the nearest matching exception handler on the call stack. ---------------------------------------------------------------------- devEric69 · 2021-11-01 09:03 Quote from: Remy Lebeau on October 30, 2021, 08:28:20 pmIf you want to ignore the next OnChange event, you will have to flag that manually, eg: 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 var  SkipNextChangeEvent: Boolean = False;[snip]  Hello Remy, Thank you for your explanations. Well, your solution is more semantic and contains less code than those of @howardpc's: so, I like it. However, it contains a global variable, which I avoid using as much as possible . Nevertheless, this global variable + your explanations about the technicality (oriented MS-Windows) of the messages distribution loop ( 'happy to know PeekMessage(PM_REMOVE); ), seems to reinforce me, that the procedure syUtils.Abort could be "improved" (in Lazarus) by doing (porting) something like..: 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 SendMessage(ieHighestPriority, LM_ABORT_PROCESS_ANY_NEW_MESSAGE);PostMessage(ieLowestPriority, LM_STOP_ABORT_PROCESS_ANY_NEW_MESSAGE);Raise EAbort.Create(SAbortError)... and that in the polymorphic (inter-OSes) " glue " loop of messages (in Lazarus) could process it like this: 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 while fpPeekMessage(reMessage) do begin    if (reMessage^.iMessage = LM_ABORT_PROCESS_ANY_NEW_MESSAGE) {having reMessage^.priority = ieHighestPriority} then          bAbort_process_any_new_message:= True;    if (reMessage^.iMessage = LM_STOP_ABORT_PROCESS_ANY_NEW_MESSAGE) {having reMessage^.priority = ieLowestPriority} then          bAbort_process_any_new_message:= False;    if bAbort_process_any_new_message then Continue {or any other equivalent of PeekMessage(PM_REMOVE)};...\... AMHO. ---------------------------------------------------------------------- Remy Lebeau · 2021-11-01 18:05 Quote from: devEric69 on November 01, 2021, 10:03:38 amThank you for your explanations. Well, your solution is more semantic and contains less code than those of @howardpc's: so, I like it. However, it contains a global variable, which I avoid using as much as possible . It doesn't have to be global, it could be a data member of the Form class instead.  Or, you could simply use the TEdit.Tag property instead, if you are not using it for something else. Quote from: devEric69 on November 01, 2021, 10:03:38 amNevertheless, this global variable + your explanations about the technicality (oriented MS-Windows) of the messages distribution loop ( 'happy to know PeekMessage(PM_REMOVE); ), seems to reinforce me, that the procedure syUtils.Abort could be "improved" (in Lazarus) by doing (porting) something like..: No, it can't.  It breaks Delphi compatibility. It is not cross- platform. And it is RARELY desirable to discard messages in this manner anyway.  It reeks of bad and unstable UI design. ---------------------------------------------------------------------- devEric69 · 2021-11-01 18:58 Quote from: Remy Lebeau on November 01, 2021, 07:05:34 pm Quote from: devEric69 on November 01, 2021, 10:03:38 amNevertheless, this global variable + your explanations about the technicality (oriented MS-Windows) of the messages distribution loop ( 'happy to know PeekMessage(PM_REMOVE); ), seems to reinforce me, that the procedure syUtils.Abort could be "improved" (in Lazarus) by doing (porting) something like..: No, it can't.  It breaks Delphi compatibility. It is not cross- platform. And it is RARELY desirable to discard messages in this manner anyway.  It reeks of bad and unstable UI design. All right: I defer to your expertise (and I've finally the answer to my primary questioning ).