System.Exception - Maybe something to do with my timer

0

So I have come to a problem that I have never had before and I cant find any information about my specific problem. Basically when i start the program i have a panel that displays time and weather information. When i start it, the weather is refreshed as well as the time with no problem (The problem starts at //ERROR HERE!!!) I think this probably has something to do with the timer because it works all fine without the timer but I need it so i can constantly refresh the time & weather. I will include the error at the end.

What could be the cause of this? If you need any more information let me know!

public async void WeatherInfoRefresh()
{
    TimeRefresh();

    RootObject myWeather = await CurrentWeather.GetWeather("Adelaide");

    ResultTextBlock.Text = myWeather.name + " • " + myWeather.main.temp + "°C";

    if (myWeather.weather[0].description == "broken clouds")
    {
    WeatherImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/Files/AAA/brokenclouds.png"));
    }
}

My timer voids:

public void startTimer()
{
    ticker = new Timer(TimerMethod, null, 30000, 3000);
}

public void TimerMethod(object state)
{
    WeatherInfoRefresh();
}

My time refresh void:

public void TimeRefresh()
{
    //Timedate
    DayTextBlock.Text = DateTime.Now.ToString("dddd"); //ERROR HERE - This almost always works without the timer!!!
    DateTextBlock.Text = DateTime.Now.ToString("dd MMMM");
    TimeTextBlock.Text = DateTime.Now.ToString("h:mm tt");
}

And the error:

An exception of type 'System.Exception' occurred in Mashy Home Automation.exe but was not handled in user code

Additional information: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
c#
windows
asked on Stack Overflow Dec 17, 2015 by mateos

1 Answer

3

The timer and UI run on different threads, you cannot update the UI from the timer thread.

answered on Stack Overflow Dec 17, 2015 by user1450877

User contributions licensed under CC BY-SA 3.0