Skip to content

Commit

Permalink
use task to refactory AbortableThread
Browse files Browse the repository at this point in the history
  • Loading branch information
MikiraSora committed Mar 2, 2024
1 parent fc13b18 commit 58f6e83
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 282 deletions.
65 changes: 41 additions & 24 deletions OngekiFumenEditor/AppBootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ControlzEx.Standard;
using Gemini.Framework.Services;
using Gemini.Modules.Output;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using OngekiFumenEditor.Base;
using OngekiFumenEditor.Base.Collections;
Expand Down Expand Up @@ -262,7 +263,6 @@ private void InitIPCServer()
})
{
Name = "OngekiFumenEditorIPCThread",
IsBackground = true,
};
ipcThread.Start();
}
Expand Down Expand Up @@ -292,12 +292,16 @@ private void InitExceptionCatcher()
{
var recHandle = new HashSet<IntPtr>();

async void ProcessException(object sender, Exception exception)
async void ProcessException(object sender, Exception exception, string trigSource)
{
if (exceptionHandling)
{
return;
}
exceptionHandling = true;

await FileLogOutput.WriteLog($"trigged by {trigSource}");

try
{
foreach (var visual in Application.Current.Windows.OfType<Window>())
Expand Down Expand Up @@ -332,41 +336,54 @@ void exceptionDump(Exception e, int level = 0)
sb.AppendLine($"----------------------------");
await FileLogOutput.WriteLog(sb.ToString());
#if !DEBUG
var exceptionHandle = Marshal.GetExceptionPointers();
var dumpFile = string.Empty;
if (exceptionHandle != IntPtr.Zero && !recHandle.Contains(exceptionHandle))
{
dumpFile = DumpFileHelper.WriteMiniDump(exceptionHandle);
recHandle.Add(exceptionHandle);
}
await FileLogOutput.WriteLog("FumenRescue.Rescue() Begin");
var resuceFolders = await FumenRescue.Rescue();
await FileLogOutput.WriteLog("FumenRescue.Rescue() End");

var logFile = FileLogOutput.GetCurrentLogFile();

var exceptionWindow = new ExceptionTermWindow(innerMessage, resuceFolders, logFile, dumpFile);
exceptionWindow.ShowDialog();

exceptionHandling = true;
Environment.Exit(-1);
var exceptionHandle = Marshal.GetExceptionPointers();
var dumpFile = string.Empty;
if (exceptionHandle != IntPtr.Zero && !recHandle.Contains(exceptionHandle))
{
dumpFile = DumpFileHelper.WriteMiniDump(exceptionHandle);
recHandle.Add(exceptionHandle);
}
await FileLogOutput.WriteLog("FumenRescue.Rescue() Begin\n");
var resuceFolders = await FumenRescue.Rescue();
await FileLogOutput.WriteLog("FumenRescue.Rescue() End\n");

var logFile = FileLogOutput.GetCurrentLogFile();

var apartmentState = Thread.CurrentThread.GetApartmentState();
await FileLogOutput.WriteLog($"current apartmentState: {apartmentState}\n");
if (apartmentState == ApartmentState.STA)
{
var exceptionWindow = new ExceptionTermWindow(innerMessage, resuceFolders, logFile, dumpFile);
exceptionWindow.ShowDialog();
}
else
{
var result = Application.Current.Invoke(() =>
{
var exceptionWindow = new ExceptionTermWindow(innerMessage, resuceFolders, logFile, dumpFile);
return exceptionWindow.ShowDialog();
});
}

exceptionHandling = true;
Environment.Exit(-1);
#else
throw exception;
#endif
}

AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
AppDomain.CurrentDomain.UnhandledException += async (sender, e) =>

Check warning on line 375 in OngekiFumenEditor/AppBootstrapper.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
ProcessException(sender, e.ExceptionObject as Exception);
ProcessException(sender, e.ExceptionObject as Exception, "AppDomain.CurrentDomain.UnhandledException");
};
Application.Current.DispatcherUnhandledException += (sender, e) =>
{
ProcessException(sender, e.Exception);
ProcessException(sender, e.Exception, "Application.Current.DispatcherUnhandledException");
e.Handled = true;
};
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
ProcessException(sender, e.Exception);
ProcessException(sender, e.Exception, "TaskScheduler.UnobservedTaskException");
e.SetObserved();
};
}
Expand Down
Loading

0 comments on commit 58f6e83

Please sign in to comment.