locked
0x8001010E (RPC_E_WRONG_THREAD)

    Question

  • Hello,

    I am getting this error from Visual Studio:

     "0x8001010E (RPC_E_WRONG_THREAD)"

    It occurs when from a roaming data changed Event the "DefconStatus" Property is going to be changed while the app is running. I guess it could be handled by the Dispatcher or async await stuff. The Change of the UI by the roaming Event is of course some Kind of an Invokation of the normal way the data changes. I would be grateful for any ideas or Solutions.

    The app itself is located here: MyDEFCON

    //Event Subscription in the ViewModel constructor
    public MainPageViewModel(INavigationService navigationService)
            {
                Windows.Storage.ApplicationData.Current.DataChanged += Current_DataChanged;
                _navigationService = navigationService;
            }
    
    //The causing property
    string _DefconStatus = default(string);
            public string DefconStatus { get { return _DefconStatus; } set { SetProperty(ref _DefconStatus, value); } }
    
    //Eventhandler
    void Current_DataChanged(ApplicationData sender, object args)
            {
                LoadDefconStatus();
            }
    
    //The load from roaming Settings method
    private void LoadDefconStatus()
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                if (roamingSettings.Values.ContainsKey("defconStatus"))
                {
                    roamingUpdate = true;
                    SetDefconStatus(Convert.ToInt16(roamingSettings.Values["defconStatus"].ToString()));
                }
                else SetDefcon5();
            }
    //Status setter called from roaming update
    private void SetDefconStatus(int status)
            {
                switch (status)
                {
                    case 1:
                        SetDefcon1();
                        break;
                    case 2:
                        SetDefcon2();
                        break;
                    case 3:
                        SetDefcon3();
                        break;
                    case 4:
                        SetDefcon4();
                        break;
                    case 5:
                        SetDefcon5();
                        break;
                    default:
                        break;
                }
            }
    
    //One of the five DEFCON setter
    private void SetDefcon1()
            {
                DefconStatus = "1";
                defconSet = true;
                Defcon1 = true;
                Defcon2 = false;
                Defcon3 = false;
                Defcon4 = false;
                Defcon5 = false;
                UpdateTileToDefcon1();
                if (!roamingUpdate)
                {
                    SaveDefconStatus();
                }
                roamingUpdate = false;
                defconSet = false;
            }

    Tuesday, February 17, 2015 11:47 PM

Answers

  • I found the solution by myself, but I would appreciate any Suggestion for code improvement.

    private async void Current_DataChanged(ApplicationData sender, object args)
            {
                await LoadDefconStatus();
            }
    
    private async Task<object> LoadDefconStatus()
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                
                if (roamingSettings.Values.ContainsKey("defconStatus"))
                {
                    roamingUpdate = true;
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    SetDefconStatus(Convert.ToInt16(roamingSettings.Values["defconStatus"].ToString())));
                }
                else SetDefcon5();
                return Task.FromResult<object>(null);
            }


    • Marked as answer by Marcus Runge Wednesday, February 18, 2015 12:27 AM
    • Edited by Marcus Runge Wednesday, February 18, 2015 12:27 AM
    Wednesday, February 18, 2015 12:26 AM

All replies

  • I found the solution by myself, but I would appreciate any Suggestion for code improvement.

    private async void Current_DataChanged(ApplicationData sender, object args)
            {
                await LoadDefconStatus();
            }
    
    private async Task<object> LoadDefconStatus()
            {
                Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
                
                if (roamingSettings.Values.ContainsKey("defconStatus"))
                {
                    roamingUpdate = true;
                    await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    SetDefconStatus(Convert.ToInt16(roamingSettings.Values["defconStatus"].ToString())));
                }
                else SetDefcon5();
                return Task.FromResult<object>(null);
            }


    • Marked as answer by Marcus Runge Wednesday, February 18, 2015 12:27 AM
    • Edited by Marcus Runge Wednesday, February 18, 2015 12:27 AM
    Wednesday, February 18, 2015 12:26 AM
  • Good job!
    Wednesday, February 18, 2015 1:54 AM