Question for the mob:

Question for the mob:

I have something like this in my main VCL Form:

for aa in AList do
for bb in BList do
for cc in CList do
xyz.DoThisAndWaitForAsyncProcess(aa.n1, bb.n2, cc.n3)

I know how to do this if it's in a thread, and I swear I've seen solutions for it in a form within the VCL, but I'm drawing a blank.

The async process could be anything that takes a while and is done in another thread, like running a DB query on a server that takes a while; doing some complex data manipulation; or in this case, playing an audio file. Things that can take many seconds if not longer to complete.

I want to to act as if it's a synchronous call in this case, but there's no synchronous API. The API call returns immediately, and there's an event handler callback that's triggered when the async process completes.

Note that there's no way to queue up all of these events first as later ones can be dependent on earlier ones. So I need to process each one and wait for it to complete before continuing the innermost loop.

Comments

  1. This is a simplified presentation of what I need to control. There's a lot more interaction with the UI that would all have to be synchronized if I moved this loop into a separate thread. The lists, for example. And some other stuff. So it's a few hoops here vs. a bunch more in other places.

    ReplyDelete
  2. David Schwartz In my experience, moving work away from the main thread and making the processing truly asynchronous, is far less complicated than trying to keep it partially asynchronous while trying to not freeze up the main thread. ProcessMessages is the road to perdition and opens a can of worms with regards to reentrancy.

    Have you had a look at the OmniThreadLibrary? It is a fabulous toolset that simplifies how you queue work and communicate between threads.

    ReplyDelete
  3. I got this working. Thanks everybody for all the help and suggestions!

    ReplyDelete

Post a Comment