TTMSFNCPageControl OnBeforeChangePage possible error

Hi,

I have an OnBeforeChangePage procedure that works as follows:

procedure TMain.TMSFNCPageControl1BeforeChangePage(Sender: TObject;
  ACurrentPageIndex, ANewPageIndex: Integer; var ACanChange: Boolean);
var
  mr:TModalResult;
begin
    mr:=await(MessageDlgAsync('Change page?', mtInformation, [mbNo, mbYes]));
  if mr=mrNo then
    ACanChange:=False
  else
    ACanChange:=True;
end;

and an OnChangePage event:

procedure TMain.TMSFNCPageControl1ChangePage(Sender: TObject;
  APreviousPageIndex, ACurrentPageIndex: Integer);
begin
  *do_something()*
end;

The OnChangePage event fires before the OnBeforeChangePage can finish, seemingly ignoring the await(). I've tried several different async routines and the behaviour happens every time.

Thanks!

The await() ensures that within the event handler code block, the code after the await() call is executed only after the in your case MessageDlgAsync() call was finished.
But await() is always limited to the code block in which it is placed and does not have an impact on the code block from where in this case OnBeforeChangePage and OnChangePage are called. This is tied to how JavaScript await/promises itself work.
So, in a nutshell, always treat await() as a mechanism to control code flow sequence within code blocks and not across code blocks or affect callers of code blocks containing await().

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.