Handle failing tasks that are not awaited

This commit is contained in:
Nikolay Kuznetsov 2021-05-17 23:15:38 +02:00
parent f755fc8b4a
commit 4b69295fda
3 changed files with 20 additions and 2 deletions

View file

@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
@ -33,5 +34,16 @@ namespace VRCMelonAssistant
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
public static void NoAwait(this Task task)
{
task.ContinueWith(t =>
{
if (t.IsFaulted)
{
Utils.ShowErrorMessageBox("Exception in free-floating task", t.Exception);
}
});
}
}
}

View file

@ -409,6 +409,12 @@ namespace VRCMelonAssistant
caller.BeginInvoke(Message, null, null, null);
}
public static void ShowErrorMessageBox(string title, Exception ex)
{
MessageBox.Show(MainWindow.Instance, $"{title}\n{ex}", "Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
/// <summary>
/// Attempts to write the specified string to the <see cref="System.Windows.Clipboard"/>.
/// </summary>

View file

@ -66,7 +66,7 @@ namespace VRCMelonAssistant
Main.Content = Intro.Instance;
break;
case "Mods":
ShowModsPage();
ShowModsPage().NoAwait();
break;
case "About":
Main.Content = About.Instance;
@ -137,7 +137,7 @@ namespace VRCMelonAssistant
private void ModsButton_Click(object sender, RoutedEventArgs e)
{
ShowModsPage();
ShowModsPage().NoAwait();
}
private void IntroButton_Click(object sender, RoutedEventArgs e)