Skip to content

Commit f5d3acb

Browse files
committed
Catch General Exception when checking update, and use PeriodicTimer to avoid error to crash the main thread
1 parent b338494 commit f5d3acb

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Flow.Launcher.Core/Updater.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Updater(string gitHubRepository)
3333

3434
public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
3535
{
36-
await UpdateLock.WaitAsync();
36+
await UpdateLock.WaitAsync().ConfigureAwait(false);
3737
try
3838
{
3939
if (!silentUpdate)
@@ -88,9 +88,13 @@ public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
8888
UpdateManager.RestartApp(Constant.ApplicationFileName);
8989
}
9090
}
91-
catch (Exception e) when (e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException)
91+
catch (Exception e)
9292
{
93-
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e);
93+
if ((e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException))
94+
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e);
95+
else
96+
Log.Exception($"|Updater.UpdateApp|Error Occurred", e);
97+
9498
if (!silentUpdate)
9599
api.ShowMsg(api.GetTranslation("update_flowlauncher_fail"),
96100
api.GetTranslation("update_flowlauncher_check_connection"));

Flow.Launcher/App.xaml.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.Text;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using System.Timers;
67
using System.Windows;
@@ -84,7 +85,7 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () =>
8485

8586
Current.MainWindow = window;
8687
Current.MainWindow.Title = Constant.FlowLauncher;
87-
88+
8889
HotKeyMapper.Initialize(_mainVM);
8990

9091
// happlebao todo temp fix for instance code logic
@@ -130,20 +131,17 @@ private void AutoStartup()
130131
//[Conditional("RELEASE")]
131132
private void AutoUpdates()
132133
{
133-
Task.Run(async () =>
134+
_ = Task.Run(async () =>
134135
{
135136
if (_settings.AutoUpdates)
136137
{
137-
// check udpate every 5 hours
138-
var timer = new Timer(1000 * 60 * 60 * 5);
139-
timer.Elapsed += async (s, e) =>
140-
{
141-
await _updater.UpdateAppAsync(API);
142-
};
143-
timer.Start();
144-
145-
// check updates on startup
138+
// check update every 5 hours
139+
var timer = new PeriodicTimer(TimeSpan.FromHours(5));
146140
await _updater.UpdateAppAsync(API);
141+
142+
while (await timer.WaitForNextTickAsync())
143+
// check updates on startup
144+
await _updater.UpdateAppAsync(API);
147145
}
148146
});
149147
}

0 commit comments

Comments
 (0)