diff --git a/Apewer.Windows/WindowsUtility.cs b/Apewer.Windows/WindowsUtility.cs
index b0bf8bf..ea721a9 100644
--- a/Apewer.Windows/WindowsUtility.cs
+++ b/Apewer.Windows/WindowsUtility.cs
@@ -163,6 +163,49 @@ namespace Apewer
catch { return false; }
}
+ /// 获取同名进程。
+ public static Process[] PreviousProcess()
+ {
+ var current = Process.GetCurrentProcess();
+
+ var result = new List();
+ var all = Process.GetProcesses();
+ foreach (var process in all)
+ {
+ if (process.Id == current.Id) continue;
+ if (process.ProcessName != current.ProcessName) continue;
+ result.Add(process);
+ }
+ return result.ToArray();
+ }
+
+ /// 结束同名进程。
+ /// 在强制结束进程前执行的方法。
+ ///
+ ///
+ ///
+ public static void KillPreviousProcess(Action before = null)
+ {
+ var current = Process.GetCurrentProcess();
+ var processes = Process.GetProcesses();
+ foreach (var process in processes)
+ {
+ if (process.Id == current.Id) continue;
+ if (process.ProcessName != current.ProcessName) continue;
+
+ before?.Invoke(process);
+ try
+ {
+ if (process.HasExited) continue;
+ process.Kill();
+ }
+ catch
+ {
+ if (!process.HasExited) throw;
+ }
+ }
+ }
+
/// 查询指定的进程 ID 是否存在。
/// 进程 ID。
public static bool ProcessIsAlive(int pid)
@@ -384,6 +427,17 @@ namespace Apewer
throw new SystemException($"向内存地址写入数据失败。");
}
+ /// 捕获异常。
+ /// 此方法内会自动调用 方法,无需重复调用。
+ public static void CatchException(Action callback)
+ {
+ RuntimeUtility.CatchException(callback);
+#if !NETFRAMEWORK
+ System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);
+ System.Windows.Forms.Application.ThreadException += (s, e) => callback(e.Exception);
+#endif
+ }
+
#endregion
#region 控制台。
diff --git a/Apewer/Apewer.csproj b/Apewer/Apewer.csproj
index 4075928..9c13265 100644
--- a/Apewer/Apewer.csproj
+++ b/Apewer/Apewer.csproj
@@ -23,6 +23,7 @@
+
@@ -31,6 +32,7 @@
+
@@ -39,6 +41,7 @@
+
diff --git a/Apewer/RuntimeUtility.cs b/Apewer/RuntimeUtility.cs
index da07d3d..48e39df 100644
--- a/Apewer/RuntimeUtility.cs
+++ b/Apewer/RuntimeUtility.cs
@@ -1,7 +1,5 @@
-using Apewer.Internals;
-using Apewer.Models;
+using Apewer.Models;
using System;
-using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@@ -9,7 +7,6 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
-using static System.Net.Mime.MediaTypeNames;
namespace Apewer
{
@@ -906,6 +903,22 @@ namespace Apewer
get { return Process.GetCurrentProcess().MainModule.FileName; }
}
+ /// 捕获异常。
+ ///
+ public static void CatchException(Action callback)
+ {
+ if (callback == null) throw new ArgumentNullException(nameof(callback));
+
+ AppDomain.CurrentDomain.UnhandledException += (s, e) => callback(e.ExceptionObject as Exception);
+#if !NET20
+ System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (s, e) => callback(e.Exception);
+#endif
+#if NETFRAMEWORK
+ System.Windows.Forms.Application.SetUnhandledExceptionMode(System.Windows.Forms.UnhandledExceptionMode.CatchException);
+ System.Windows.Forms.Application.ThreadException += (s, e) => callback(e.Exception);
+#endif
+ }
+
#endregion
#region Evaluate