[英]Why unhandled exception in a background thread doesnt crash the app domain?
[英]how to crash the main process if there is unhandled exception inside background thread
我有一個發布者,調度員,訂閱者。
發布者通過調度程序向訂閱者發布事件。
訂戶是嵌入在S.exe中的com對象,當特定事件來自Publisher.exe時可以從dispatcher.exe進行回調。
我希望訂戶內的任何異常都將終止S.exe。
我做了調查:
具有配置的任務可以終止主進程。 相關文章https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskscheduler.unobservedtaskexception?view=netframework-4.7.2
在我的代碼中新建一個線程,沒有任何特定的配置也可以,線程內部任何未處理的異常都可能使主進程崩潰。 相關文章, https://docs.microsoft.com/zh-cn/dotnet/standard/threading/exceptions-in-managed-threads
一個線程的兩個屬性:{isbackground,isThreadPoolThread}。
是否還有其他配置(例如)可以設置為控制是否使主進程崩潰?
您可以處理AppDomain.CurrentDomain.UnhandledException
事件,在該事件中,您可以使用Process.GetCurrentProcess()
獲取當前進程並殺死該進程,以便您的exe
終止。
將此行Main()
放在Program.cs
:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException
然后,如下所示實現CurrentDomain_UnhandledException
。
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var currentProcess = Process.GetCurrentProcess();
currentProcess.Kill();
}
根據您的問題陳述,這只是概念。 您可以根據需要進行更改。
RCW將COM內的任何異常包裝為HRESULT返回給其客戶端。 因此,當在COM線程中引發異常時,無法使主進程崩潰。
答案可能與以下有關: COM方法通過返回HRESULT報告錯誤; .NET方法通過拋出異常來報告它們。 運行時處理兩者之間的過渡。 .NET Framework中的每個異常類都映射到一個HRESULT。
來自https://docs.microsoft.com/zh-cn/dotnet/framework/interop/how-to-map-hresults-and-exceptions
和其他有用的鏈接: https : //docs.microsoft.com/zh-cn/dotnet/framework/interop/runtime-callable-wrapper
https://docs.microsoft.com/en-us/dotnet/framework/interop/com-callable-wrapper
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.