I am creating a Windows 8.1 Phone App. The UI has a button and a TextBox (called txtStatus)
Basically, when I click a button in the UI, the following code kicks off (only some of it is shown):
private async void btnStart_Click(object sender, RoutedEventArgs e)
{
try
{
// Get the list of Microsoft Bands paired to the phone.
IBandInfo[] pairedBands = await BandClientManager.Instance.GetBandsAsync();
if (pairedBands.Length < 1)
{
txtStatus.Text = "This sample app requires a Microsoft Band paired to your device. Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.";
return;
}
// Connect to Microsoft Band.
using (IBandClient bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
{
bandClient.SensorManager.HeartRate.ReadingChanged += HeartRate_ReadingChanged;
await bandClient.SensorManager.HeartRate.StartReadingsAsync();
// Receive Accelerometer data for a while, then stop the subscription.
await Task.Delay(TimeSpan.FromSeconds(50));
await bandClient.SensorManager.HeartRate.StopReadingsAsync();
}
}
catch (Exception ex)
{
}
private void HeartRate_ReadingChanged(object sender, Microsoft.Band.Sensors.BandSensorReadingEventArgs<Microsoft.Band.Sensors.IBandHeartRateReading> e)
{
txtStatus.Text = string.Format("Current Heart Rate is: {0}", e.SensorReading.HeartRate.ToString());
}
When I run this code, it barfs at the following line in the handler:
txtStatus.Text = string.Format("Current Heart Rate is: {0}", e.SensorReading.HeartRate.ToString());
The exception message is as follows:
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
It sounds like the UI thread and the Sensor reading thread are different. Any suggestions on how to use the same thread for both. Or how to pass data between the two threads?
Thanks in anticipation.