# Windows Background thread execution?

**URL:** <https://forum.xojo.com/t/windows-background-thread-execution/46088>\
**Category:** Windows\
**Created:** [November 2, 2018, 4:10pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088 "2018-11-02T16:10:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 2, 2018, 4:10pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/1 "2018-11-02T16:10:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [November 2, 2018, 4:28pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/2 "2018-11-02T16:28:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 2, 2018, 4:38pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/3 "2018-11-02T16:38:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [November 2, 2018, 5:42pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/4 "2018-11-02T17:42:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 2, 2018, 6:50pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/5 "2018-11-02T18:50:58Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [November 2, 2018, 7:06pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/6 "2018-11-02T19:06:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:26pm UTC](https://forum.xojo.com/t/windows-background-thread-execution/46088/7 "2020-10-29T19:26:43Z")

</div>


