Search code examples
windows-8windows-runtimewinrt-xamlwinrt-async

Error in Windows 8 xaml Modern app with thread


I have following two methods. When user clicks on start button from ui, the step geoLocator_PositionChanged in geoLocator_PositionChanged method is fired and calls the other method geoLocator_PositionChanged.But when it comes to try block while executing the first statement it throws the following error:

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

private async void btnStartStop_Click_1(object sender, RoutedEventArgs e)

    {
      geoLocator.PositionChanged += geoLocator_PositionChanged; 
     }

async void geoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)

    {

        MessageDialog msgdlg = null;
        bool bDisplayDialog = false;
        try
        {
            lblAltValue.Text = args.Position.Coordinate.Altitude.ToString();  
    }
    catch
    {
    }
    }

Any help how can I fix this issue ?


Solution

  • You try to access the UI-Thread from another one.

    Try something like ths

    Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
    (Windows.UI.Core.CoreDispatcherPriority.Normal, () => 
    {
        //HERE GOES THE UI ACCESS LIKE this.textbox.text = "MY AWESOME TEXT"; 
    });