fix: #6349 put external notification job in dedicated async task context

This commit is contained in:
Liam Sho
2023-09-17 16:11:02 +08:00
parent 084fefc7b5
commit cd9b8a5089
3 changed files with 20 additions and 11 deletions

View File

@@ -627,7 +627,7 @@ namespace MaaWpfGui.Main
.Replace("{Datetime}", DateTime.Now.ToString("U"))
.Replace("{Preset}", configurationPreset);
ExternalNotificationService.SendAsync(allTaskCompleteTitle, allTaskCompleteMessage).Wait();
ExternalNotificationService.Send(allTaskCompleteTitle, allTaskCompleteMessage);
using (var toast = new ToastNotification(allTaskCompleteTitle))
{
toast.Show();

View File

@@ -11,6 +11,7 @@
// but WITHOUT ANY WARRANTY
// </copyright>
using System.Collections.Generic;
using System.Threading.Tasks;
using MaaWpfGui.Constants;
using MaaWpfGui.Helper;
@@ -19,14 +20,9 @@ namespace MaaWpfGui.Services.Notification
{
public static class ExternalNotificationService
{
/// <summary>
/// Send notification
/// </summary>
/// <param name="title">The title of the notification</param>
/// <param name="content">The content of the notification</param>
/// <param name="isTest">Indicate if it is a test or not.</param>
/// <returns>Async task</returns>
public static async Task SendAsync(string title, string content, bool isTest = false)
private static readonly List<Task> _taskContainers = new List<Task>();
private static async Task SendAsync(string title, string content, bool isTest = false)
{
var enabledProvider = ConfigurationHelper.GetValue(ConfigurationKeys.ExternalNotificationEnabled, "Off");
@@ -47,5 +43,18 @@ namespace MaaWpfGui.Services.Notification
using var toast = new ToastNotification(LocalizationHelper.GetString(result ? "ExternalNotificationSendSuccess" : "ExternalNotificationSendFail"));
toast.Show();
}
/// <summary>
/// Send notification
/// </summary>
/// <param name="title">The title of the notification</param>
/// <param name="content">The content of the notification</param>
/// <param name="isTest">Indicate if it is a test or not.</param>
public static void Send(string title, string content, bool isTest = false)
{
var task = SendAsync(title, content, isTest);
_taskContainers.RemoveAll(x => x.Status != TaskStatus.Running);
_taskContainers.Add(task);
}
}
}

View File

@@ -152,9 +152,9 @@ namespace MaaWpfGui.ViewModels.UI
#region External Notifications
public async Task ExternalNotificationSendTest()
public void ExternalNotificationSendTest()
{
await ExternalNotificationService.SendAsync(
ExternalNotificationService.Send(
LocalizationHelper.GetString("ExternalNotificationSendTestTitle"),
LocalizationHelper.GetString("ExternalNotificationSendTestContent"),
true);