DispatcherTimer Error

2

I am writing a Windows Phone 8.1 App (WINRT).

I am showing a StatusBar:

  1. MYAPP (WELCOME)
  2. MYAPP (CONNECTED VIA WIFI)
  3. MYAPP (CONNECTED VIA CELLULAR)

If user is connected via Wifi, it shows "MYAPP (CONNECTED VIA WIFI)" and if user is connected via cellular, it shows "MYAPP (CONNECTED VIA CELLULAR)".

Also, it restores to default progressbartext "MYAPP (WELCOME)" after 5 seconds by using DispatcherTimer();


So,I execute GetInternetConnectionProfile() , returns _connectedVia, passing to GetInternetConnectionProfile(_connectedVia) function in Constructor or OnNavigatedTo.

Connectivity.GetInternetConnectionProfile();
StatusBarClassObject.StatusBarGenerator(_connectedVia);

Everything works fine till first execution of page.

Now, everytime a network change hanppens (Wifi to cellular or viceversa or wifi to none, etc), it executes NetworkStatusChanged eventhandler with same process as in constructor.

    NetworkInformation.NetworkStatusChanged += new NetworkStatusChangedEventHandler((n) => Connectivity.GetInternetConnectionProfile());

   NetworkInformation.NetworkStatusChanged += new NetworkStatusChangedEventHandler((n) => StatusBarClassObject.StatusBarGenerator(_connectedVia));

But the problem is that: StatusBarCloseTimer = new DispatcherTimer(); is giving me error this time. First execution of this works fine.

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

I tried to use:

await CoreDispatcherObject.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
         DispatcherTimerInit();
        });



private void DispatcherTimerInit()
            {
        //  throw new NotImplementedException();
            StatusBarCloseTimer = new DispatcherTimer();
            StatusBarCloseTimer.Interval = TimeSpan.FromMilliseconds(3000);
            StatusBarCloseTimer.Tick += StatusBarCloseTimer_Tick;
            }  

But now StatusBarCloseTimer.Start(); is giving me same error: "The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))"

c#
multithreading
xaml
oop
windows-phone-8.1
asked on Stack Overflow Jan 30, 2015 by Atif Shabeer • edited Jun 20, 2020 by Community

1 Answer

2

Solved: await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.N‌​ormal, () => {}

answered on Stack Overflow Jan 30, 2015 by Atif Shabeer

User contributions licensed under CC BY-SA 3.0