Skip to content

Commit

Permalink
优化天气加载性能
Browse files Browse the repository at this point in the history
  • Loading branch information
STBBRD committed Jul 20, 2024
1 parent 65e08a3 commit 2d54c85
Showing 1 changed file with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public WeatherPage()
{
InitializeComponent();

Timer_Tick(null,null);
Timer_Tick(null, null);

timer.Interval = TimeSpan.FromHours(1);
timer.Tick += Timer_Tick;
Expand All @@ -43,45 +43,53 @@ public WeatherPage()

private LiveWeather liveWeather = new LiveWeather();

private void Timer_Tick(object sender, EventArgs e)
private async void Timer_Tick(object sender, EventArgs e)
{
if (!new DirectoryInfo("Weather/").Exists)
{
try { new DirectoryInfo("Weather/").Create(); }
catch { }
}

if (File.Exists(liveWeatherFilePath))
timer.Stop();
await Task.Run(() =>
{
DateTime liveWeatherFetchTime = new FileInfo(liveWeatherFilePath).LastWriteTime;
if (DateTime.Now - liveWeatherFetchTime > TimeSpan.FromHours(1))
if (!new DirectoryInfo("Weather/").Exists)
{
UpdateLiveWeather();
try { new DirectoryInfo("Weather/").Create(); }
catch { }
}
else
{
liveWeather = JsonConvert.DeserializeObject<LiveWeather>(File.ReadAllText(liveWeatherFilePath));

if (liveWeather.adcode != Weather.GetCityCode(MainWindow.Settings.InfoBoard.WeatherCity))
if (File.Exists(liveWeatherFilePath))
{
DateTime liveWeatherFetchTime = new FileInfo(liveWeatherFilePath).LastWriteTime;
if (DateTime.Now - liveWeatherFetchTime > TimeSpan.FromHours(1))
{
UpdateLiveWeather();
if (File.Exists("Weather/CastWeather.json"))
{
File.Delete("Weather/CastWeather.json");
}
}
else if (liveWeather.isError)
else
{
UpdateLiveWeather();
liveWeather = JsonConvert.DeserializeObject<LiveWeather>(File.ReadAllText(liveWeatherFilePath));

if (liveWeather.adcode != Weather.GetCityCode(MainWindow.Settings.InfoBoard.WeatherCity))
{
UpdateLiveWeather();
if (File.Exists("Weather/CastWeather.json"))
{
File.Delete("Weather/CastWeather.json");
}
}
else if (liveWeather.isError)
{
UpdateLiveWeather();
}
}
}
}
else
{
UpdateLiveWeather();
}
else
{
UpdateLiveWeather();
}

ShowLiveWeather();
Dispatcher.BeginInvoke(() =>
{
ShowLiveWeather();
});
});
timer.Start();
}

private void UpdateLiveWeather()
Expand Down

0 comments on commit 2d54c85

Please sign in to comment.