系统异常-也许和我的计时器有关

本文关键字:计时器 我的 异常 也许 系统 | 更新日期: 2023-09-27 18:27:19

所以我遇到了一个以前从未遇到过的问题,我找不到任何关于我的特定问题的信息。基本上,当我启动程序时,我有一个显示时间和天气信息的面板。当我启动它时,天气和时间都被刷新了,没有问题(问题从//ERROR HERE开始!!)我认为这可能与计时器有关,因为没有计时器它一切都很好,但我需要它,这样我就可以不断刷新时间&天气我将把错误写在最后。

这可能是什么原因造成的?如果你需要更多信息,请告诉我!

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"));
    }
}

我的计时器无效:

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

我的时间刷新无效:

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");
}

错误:

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))

系统异常-也许和我的计时器有关

计时器和UI在不同的线程上运行,不能从计时器线程更新UI。