Windows Phone Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)

1

My Windows Runtime Application reads a NDEF NFC-Tag. When the App reads the NFC-Tag correct my method message receivedwill open.

private void messageReceived(ProximityDevice sender, ProximityMessage message)
{
    device.StopSubscribingForMessage(NFCID);
    var ndefMessage = NdefMessage.FromByteArray(message.Data.ToArray());

    StringBuilder sb = new StringBuilder();
    foreach (NdefRecord record in ndefMessage) sb.AppendLine(Encoding.UTF8.GetString(record.Payload, 0, record.Payload.Length));

    String data= sb.ToString();
    ShowData(data);
} 

private void ShowData(string data)
{
    tbx.Text = data;
}

When I want to set this data to a textfield, every time a exception is thrown: Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)

c#
windows-phone-8
windows-runtime
windows-phone-8.1
windows-phone
asked on Stack Overflow Aug 18, 2015 by ersu • edited Aug 22, 2017 by Vadim Kotov

1 Answer

4

You need to dispatch it first:

Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            tbx.Text = data;
        });

EDIT: Obviously this is not always the best solution. Do it this way instead if you still receive that error: Run code on UI thread in WinRT

answered on Stack Overflow Aug 18, 2015 by Wosi • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0