Wait for all Queue events done?

136 views Asked by At

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?

1

There are 1 answers

3
Remy Lebeau On BEST ANSWER

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.