c++builderrad-studioc++builder-11-alexandria

Wait for all Queue events done?


When running a function asynchronously in the main thread, like

TThread::CurrentThread->Queue(NULL, somefunction);

before closing my app I'd like to make sure all events have finished. But all I can find is a way to remove pending events

TThread::CurrentThread->RemoveQueuedEvents(somefunction);

How do I wait for them to finish instead of removing all?


Solution

  • Unfortunately, the RTL doesn't really provide a mechanism to do what you are asking for, at least not directly.

    You can try using a thread-safe counter that is incremented each time a function is queued, and decremented each time a queued function exits. Then, the main thread can call CheckSynchronize() in a loop until that counter reaches 0.

    Or, you can simply terminate all of your worker threads, make sure they are fully terminated, and then just call CheckSynchronize() 1 time to process the final queue since nothing would be adding new entries to it anymore.