Jump to content
omnibrain

Async/Await with updating visual controls

Recommended Posts

I implemented the Async/Await-Pattern as described in The Delphi Geek: Async/Await in Delphi

Now I want to update GUI-Elements from my async task. For example to write into a log window or update a status bar. How would I best do that.

 

I guess I can't use TThread.Synchronize because that's a whole different library?


memo1.append(datetimetostr(now)+': Starting'); 
Async(
    procedure begin
    memo1.append(datetimetostr(now)+': Start Processing'); 
    for i=0 to itemcount do
	begin
    //process
    TThread.Synchronize(nil,
                procedure
                begin
                  memo1.append(datetimetostr(now)+': Processed item No '+inttostr(i);                  
                end);
    end;            
   end).
  Await(
    procedure begin
      memo1.append(datetimetostr(now)+': Finished Processing');     
    end);
end;

 

Share this post


Link to post
5 minutes ago, omnibrain said:

I guess I can't use TThread.Synchronize because that's a whole different library?

I do not see why you would not be able to use it. Did you see whether your example worked?

Share this post


Link to post
10 minutes ago, omnibrain said:

I guess I can't use TThread.Synchronize because that's a whole different library?

TThread.Synchronize and TThread.Queue are parts of core RTL functionality. You can use them and they are appropriate way to synchronize any Delphi code with main thread, regardless of what other libraries you are using.

 

BTW, following line also needs to be synchronized with main thread.

memo1.append(datetimetostr(now)+': Start Processing');
Edited by Dalija Prasnikar
  • Like 1

Share this post


Link to post
2 minutes ago, Dalija Prasnikar said:

TThread.Synchronize and TThread.Queue are parts of core RTL functionality. You can use them and they are appropriate way to synchronize any Delphi code with main thread, regardless of what other libraries you are using.

Thanks a lot. It's my first real foray into parallelism and concurrency with Delphi.

 

5 minutes ago, Dave Nottage said:

I do not see why you would not be able to use it. Did you see whether your example worked?

Especially with parallelism and concurrency I learned the hard way, that there is a difference between "seems to work in development" and "does really what it should be doing", especially when the Dev is inexperienced with the tools.

 

4 minutes ago, Dalija Prasnikar said:

BTW, following line also needs to be synchronized with main thread.

Oh, yes, of course.

Share this post


Link to post

In addition I would pre-calculate the timestamp (Now) inside the thread. Otherwise you'll get the time stamp when the main thread handles the call. Not sure if this is actually significant, though.

Share this post


Link to post
1 hour ago, Uwe Raabe said:

In addition I would pre-calculate the timestamp (Now) inside the thread. Otherwise you'll get the time stamp when the main thread handles the call. Not sure if this is actually significant, though.

Doesn't matter in this case, because it's just an example, but definitily something to consider if it plays a role.

Share this post


Link to post
24 minutes ago, omnibrain said:

Doesn't matter in this case, because it's just an example, but definitily something to consider if it plays a role.

 

Today it's an example. Tomorrow you or somebody else sees the code and use it in a real application. Copy paste errors are always a fun thing to search for or debug.

  • Like 1

Share this post


Link to post
4 hours ago, Primož Gabrijelčič said:

use TThread.Queue

I do 🙂 

Both to deliver tasks to worker threads, and to collect the updates and results.

Share this post


Link to post

But all the time you need to have in mind that Queue will execute code later and avoid using in that code some objects or whatever if there is a chance they will not be there when the code executes.

 

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
×