Home > Delphi, OP4JS > Delphi delayed callback function

Delphi delayed callback function

November 13, 2013 Leave a comment Go to comments

When I wrote the Smart Mobile Studio RTL I added a handy function for delayed callback. Under JavaScript this is very easy to achieve and it’s also an important part of an environment that is based on events. In JavaScript everything is about events and all data is received in a non-blocking manner.

But the function is equally handy under Delphi, especially if you want something to execute “after im done with this”. It can be anything from displaying a hint on a form, to start loading data just a few seconds later. Or if you just want to add a form-friendly delay.

NOTE: This code has been replaced by a “non blocking” callback method, click here to view the latest post!

Here is the Delphi implementation of Smart Pascal’s w3_callback function (with a twist).
Note: This function is semi-blocking in the sense that it will halt execution inside the running procedure – but still allow the GUI to be responsive and events to fire. The alternative would be to spawn a thread with a synchronized re-entry.

Procedure w3_Callback(const aCallback:TProc;
          const ms:Integer);
var
  mCount: Integer;
  mThen:  TDateTime;
begin
  if assigned(application)
  and not application.Terminated then
  Begin
    try
      mCount:=0;
      mThen:=dateutils.IncMilliSecond(now,ms);
      repeat
        Sleep(0);
        Inc(mCount);
        if (mCount mod 600)=599 then
        begin
          if Assigned(Application)
          and not (application.Terminated) then
          application.ProcessMessages else
          break;
        end;
      until CompareDateTime(Now,mThen)>=0;
      if Assigned(Application)
      and not (application.Terminated)
      and Assigned(aCallback) then
      aCallback;
    except
      on exception do;
    end;
  end;
end;

How do you use it? With anonymous procedures ofcourse, like this:

  w3_Callback(
    procedure ()
    begin
      showmessage('Your helmet is ready lord vader');
    end,
    1000);
Advertisements
  1. November 13, 2013 at 8:49 pm

    Application.ProcessMessages aaaargghhhhhhhh!

    • Jon Lennart Aasenden
      November 14, 2013 at 7:32 am

      I should have pointed out that it doesnt block events. It is blocking to the current procedure in that you can use it to delay execution, but it keeps the GUI alive

  2. November 14, 2013 at 2:35 am

    You could also use OmniThreadLibrary:

    Parallel.Async(
    procedure
    begin
    // executed in background thread
    Sleep(1000);
    ShowMessage(‘Your helmet is ready lord vader’);
    end);

    see http://www.thedelphigeek.com/2011/04/simple-background-tasks-with.html

    • Jon Lennart Aasenden
      November 14, 2013 at 7:46 am

      Yes that should be an exact replica of the w3_callback 🙂

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: