Jump to content
stevegill

Using Parameters with Async/Await.

Recommended Posts

I'm using Async and Await with two methods.

 

The CheckForDueRemindersProcess method has two var parameters.  The intention is to run this in a thread and receive the values via the parameters.

 

The values are then passed to the CheckForDueReminderFinish method, which updates VCL components.

 

Is the following code the correct way to do this?

 

var
   RemindersDue: Boolean;
   RemindersCount: integer;
begin
   Async(procedure
         begin
            CheckForDueRemindersProcess(RemindersDue, RemindersCount);
         end).Await(procedure
                    begin
                       CheckForDueRemindersFinish(RemindersDue, RemindersCount);
                    end);

end;

 

Another question: is it ok to declare the variables as shown above in the calling method, or should they be declared like this:

 

    Async(procedure
          var
             RemindersDue: Boolean;
             RemindersCount: integer;
          begin
             CheckForDueRemindersProcess(RemindersDue, RemindersCount);
          end).Await(procedure
                     begin
                        CheckForDueRemindersFinish(RemindersDue, RemindersCount);
                     end);

 

Thanks.

 

= Steve

 

Share this post


Link to post

The first approach is OK. The second code fragment would not compile as the anonymous method in Await wouldn't know anything about RemindersDue and RemindersCount.

Share this post


Link to post
35 minutes ago, Primož Gabrijelčič said:

The first approach is OK. The second code fragment would not compile as the anonymous method in Await wouldn't know anything about RemindersDue and RemindersCount.

Thanks Primoz.  Now that I've taken another look at the second code example it's pretty obvious.

 

In the first code snippet, if I dynamically created and destroyed database components in the Async part of the code, I'm assuming that they would be completely isolated from the main thread.  Is that correct?

 

= Steve

Share this post


Link to post
43 minutes ago, stevegill said:

In the first code snippet, if I dynamically created and destroyed database components in the Async part of the code, I'm assuming that they would be completely isolated from the main thread.  Is that correct?

 

Indeed it is.

Share this post


Link to post

Thanks Primoz!

 

I love the Omnithread library.  It makes things a lot easier.

 

= Steve

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×