Browse Source

增加 CatchException,用于捕获异常。

master
王厅 1 week ago
parent
commit
4df487ef58
  1. 54
      Apewer.Windows/WindowsUtility.cs
  2. 3
      Apewer/Apewer.csproj
  3. 21
      Apewer/RuntimeUtility.cs

54
Apewer.Windows/WindowsUtility.cs

@ -163,6 +163,49 @@ namespace Apewer
catch { return false; }
}
/// <summary>获取同名进程。</summary>
public static Process[] PreviousProcess()
{
var current = Process.GetCurrentProcess();
var result = new List<Process>();
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();
}
/// <summary>结束同名进程。</summary>
/// <param name="before">在强制结束进程前执行的方法。</param>
/// <exception cref="System.ComponentModel.Win32Exception" />
/// <exception cref="NotSupportedException" />
/// <exception cref="InvalidOperationException" />
public static void KillPreviousProcess(Action<Process> 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;
}
}
}
/// <summary>查询指定的进程 ID 是否存在。</summary>
/// <param name="pid">进程 ID。</param>
public static bool ProcessIsAlive(int pid)
@ -384,6 +427,17 @@ namespace Apewer
throw new SystemException($"向内存地址写入数据失败。");
}
/// <summary>捕获异常。</summary>
/// <remarks>此方法内会自动调用 <see cref="RuntimeUtility.CatchException"/> 方法,无需重复调用。</remarks>
public static void CatchException(Action<Exception> 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 控制台。

3
Apewer/Apewer.csproj

@ -23,6 +23,7 @@
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net20'">
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<!-- .NET Framework 4.0 -->
@ -31,6 +32,7 @@
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<!-- .NET Framework 4.6.1 -->
@ -39,6 +41,7 @@
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<Reference Include="System.Web" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<!-- .NET Core 3.1 -->

21
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; }
}
/// <summary>捕获异常。</summary>
/// <exception cref="ArgumentNullException" />
public static void CatchException(Action<Exception> 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

Loading…
Cancel
Save