|
|
@ -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 控制台。
|
|
|
|