omnibrain 15 Posted March 6, 2023 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
Dave Nottage 557 Posted March 6, 2023 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
Dalija Prasnikar 1396 Posted March 6, 2023 (edited) 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 March 6, 2023 by Dalija Prasnikar 1 Share this post Link to post
omnibrain 15 Posted March 6, 2023 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
Primož Gabrijelčič 223 Posted March 6, 2023 Yes, like that, but use TThread.Queue. There's no need to block the worker thread while memo is being updated. 1 2 Share this post Link to post
Uwe Raabe 2057 Posted March 6, 2023 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
omnibrain 15 Posted March 6, 2023 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
Lajos Juhász 293 Posted March 6, 2023 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. 1 Share this post Link to post
Lars Fosdal 1792 Posted March 6, 2023 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
Cristian Peța 103 Posted March 7, 2023 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