Windows Background thread execution?

I have problems using the background event features of a device that connects via Bluetooth on Windows.
The C API that it comes with uses a rather uncommon approach:
The app has to register for receiving the events, but the API method does not expect any delegate to call then. Instead, it registers the thread as eligible and then one has to use another method, AwaitAsynchronousEvent (with an optional timeout value) that returns after the registered event occurred (or the timeout).

Xojo threads are no help in this matter: I can register during thread.run, but as there is only the AwaitMethod, the thread will not yield until the method finishes. So the GUI is still blocked.

I thought about using console helper apps, but then the API returns a DLL initialization error. Obviously only one program can access the API at one time. I had the same when I had supplier demo apps running and tried to access the device with my solution.

So a (yes, I know, unsupported) way would be to call the register and Await methods from a real background thread. There are API features like core dispatcher, and I think I even saw a “perform on background” method somewhere. Has this ever been done? Would this be a workable approach or doesn’t it work at all?

Or are there other approaches to get a thread to yield when it is just waiting for one method result?

Have you considered setting a timeout on the call and then looping to call AwaitAsynchronousEvent in a Xojo thread?

No, I didn’t. Thanks, that could work!
Edit: It DOES work!

You might also add a “Me.Sleep(15, False)” at the bottom of the loop to keep the UI even more responsive.

In general, yes. But I have to keep up several of these at the same time, and they should not miss events – a YieldToOtherThreads seems to do the job quite nicely, while the While/Wend Loop in the event alone does not always yield obviously (just like the documentation says).

Same effect, I just wasn’t thinking multiple thread instances. :slight_smile: