none
How to update a text box from within an event call using the eventargs RRS feed

  • Question

  • Hello all

    I believe I have a problem with threading. I have a "bluetooth le advertisement received" event which then manipulates the bluetooth event data and updates various text blocks. I have tried calling the code asynchronously and marshalling the UI thread in various different ways, but after 60 seconds or so my app stops - actually the app is running but several threads have exited (0x0) and now nothing is happening. If I run the same code on a less powerful machine, the app stops more quickly. Here is my current code with some sections shortened for clarity.

    public MainPage() { this.InitializeComponent(); //Initialise structures beacon_data = new Beacon_tag(0, 0, 0, 0, 0, 0); BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher(); watcher.Received += OnAdvertisementReceived; watcher.ScanningMode = BluetoothLEScanningMode.Active; watcher.Start(); } private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) { string manufacturerDataString; string strMessage; await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { //Manipulate packet data from eventArgs

    // { // reader.ReadBytes(data); // } strMessage = manufacturerDataString; //Debug Debug.WriteLine(manufacturerDataString); //Extract data from packet // //Find other useful information //UPDATE screen using interpreted data from eventArgs //Raw string tbRawData.Text = strMessage; //Radial gauge //etc //Others //etc //bluetooth packet data from eventArgs //etc //Display number of packets received tbUpdateCounter.Text = UpdateCounter.ToString(); } }); }

    If I remove the text box updates, I do not need to use a background call and the debug statement outputs continuously.

    Thanks for any pointers. I appreciate this is an often asked topic, but having tried 5 or so snippets from different forums and the MS docs I still have the problem.

    Ray


    ------

    Wednesday, July 17, 2019 12:29 PM

Answers

  • ***UPDATE***

    I have managed to fix my immediate issue by deploying a Debug version to the display laptop. This seems to be working at the moment.

    For future reference, I attempted various changes with the Release deployment package, including unchecking "Optimize code", unchecking "Compile with .NET Native tool chain" and trying to deploy for x64 instead of x86. Whatever the issue is, it appears on my development machine and display machine when the app is built in Release mode and not in Debug mode. The Windows "Bluetooth Advertisement Sample" works on my development machine in Release and Debug builds but does NOT work on the display machine in Release mode. I don't know what causes this behaviour but I'm assuming it is something to do with the differences between the hardware configuration of the two machines and something else in the compilation differences between Release and Debug builds.

     


    ------

    • Marked as answer by larsty Thursday, July 18, 2019 10:38 AM
    Thursday, July 18, 2019 10:38 AM

All replies

  • Hi larsty,

    I could not find issues in your code. Why did you think your app stops? Have you added break point on the 'OnAdvertisementReceived' event handler method to check if the 'Received' event is fired? If the 'Received' event is not fired at all, you could not think your app stops, it's just because no Bluetooth LE advertisements are being received.

    You also could test the official Bluetooth advertisement sample to see if it does work on your machine.

    Best Regards,

    Xavier


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 2:16 PM
    Moderator
  • Hi Xavier

    Thanks for the response. I have been round and round with this problem so many times I think I might be missing the obvious. I will investigate the "OnAdvertisementReceived" events. I know that they are not triggered when the updates stop, but my hunch was that something in the textbook update code was causing an issue to stop the packet handler working (i.e. the threads that are exiting). My evidence for this was that when I remove the textbox update code, the app never stops.

    I have tried your suggestion with the "Bluetooth advertisement sample. I had looked at this but couldn't get it to work due to my old version of Windows 10, but I have got round that now and the project compiles and runs despite numerous errors. On my development machine, in debug mode, the demonstration app (modified to read my beacon data) works fine - like my own app. However, as soon as I build a release package and install it on my test laptop, the exact same symptoms are observed - the app runs for a few seconds and then just stops displaying data. I can still interact with the screen, so the app has not crashed.

    I have set the MS demo to run as a foreground watcher with my beacon details. When the app stops, if I stop / restart the watcher from the same app screen nothing happens. But, if I change another screen, e.g. Foreground publisher, and then back to Foreground watcher, the listbox is now empty and clicking Run will start logging packets again for a few seconds. So perhaps something is happening to the watcher. I am now investigating this further.

    Thanks for your help, any further comments are much appreciated. This seems to be heading away from my original subject for this thread, but I will post back with results.

    Thanks

    Ray

     



    ------

    Thursday, July 18, 2019 8:25 AM
  • UPDATE

    "Bluetooth Advertisement Sample" app works fine on my development machine in Release and Debug configurations. My app works fine on my development machine in Debug configuration, but freezes when run in Release configuration (with no code changes). The Release and Debug configuration build settings are the same for both apps...

    I am confused as to how to debug this now.


    ------


    • Edited by larsty Thursday, July 18, 2019 9:03 AM
    Thursday, July 18, 2019 9:00 AM
  • ***UPDATE***

    I have managed to fix my immediate issue by deploying a Debug version to the display laptop. This seems to be working at the moment.

    For future reference, I attempted various changes with the Release deployment package, including unchecking "Optimize code", unchecking "Compile with .NET Native tool chain" and trying to deploy for x64 instead of x86. Whatever the issue is, it appears on my development machine and display machine when the app is built in Release mode and not in Debug mode. The Windows "Bluetooth Advertisement Sample" works on my development machine in Release and Debug builds but does NOT work on the display machine in Release mode. I don't know what causes this behaviour but I'm assuming it is something to do with the differences between the hardware configuration of the two machines and something else in the compilation differences between Release and Debug builds.

     


    ------

    • Marked as answer by larsty Thursday, July 18, 2019 10:38 AM
    Thursday, July 18, 2019 10:38 AM