5
votes

I am trying to get the heart rate from a Microsoft Band. It should be updating whenever the value changes. I am then trying to display that value in a TextBlock. I first create an instance of IBandClient, and set its HeartRate.ReadingChanged method like this:

bandClient.SensorManager.HeartRate.ReadingChanged += HeartRate_ReadingChanged;

Then I try to update the value like this:

private void HeartRate_ReadingChanged(object sender, Microsoft.Band.Sensors.BandSensorReadingEventArgs<Microsoft.Band.Sensors.IBandHeartRateReading> e)
{
    HeartRate = e.SensorReading.HeartRate;
}

HeartRate is an int set like so:

public int HeartRate
{
    get { return (int)GetValue(HeartRateProperty); }
    set { SetValue(HeartRateProperty, value); }
}

// Using a DependencyProperty as the backing store for HeartRate.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty HeartRateProperty =
    DependencyProperty.Register("HeartRate", typeof(int), typeof(MainPage), new PropertyMetadata(0));

The TextBlock text is then bound to HeartRate. However, I keep getting this error when trying to set HeartRate:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

My guess is that it's trying to set HeartRate while it is still being set from the call before.

1
Do you know if the SensorManager is dispatching the event from another thread? That would mean you're trying to set your TextBlock text from a different thread than the UI thread, which you can't do (hence the error).y2bd

1 Answers

3
votes

Try to implement this and see how it goes, if you still want your int variable, then convert it back to string when displaying it in a text-block.

await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
               () => 
               {
                   Textblock.Text =  e.SensorReading.HeartRate.ToString())
               }).AsTask();