简体   繁体   English

如果后台线程中有未处理的异常,如何使主进程崩溃

[英]how to crash the main process if there is unhandled exception inside background thread

I have a publisher, dispatcher, subscriber. 我有一个发布者,调度员,订阅者。

publisher publish event through dispatcher to subscriber. 发布者通过调度程序向订阅者发布事件。

subscriber is a com object embedded in S.exe can be callback from dispatcher.exe when specific event coming from publisher.exe. 订户是嵌入在S.exe中的com对象,当特定事件来自Publisher.exe时可以从dispatcher.exe进行回调。

I expect any exception inside the subscriber to terminate the S.exe. 我希望订户内的任何异常都将终止S.exe。

I did my investigation: 我做了调查:

  1. task, with configuration can terminate the main process. 具有配置的任务可以终止主进程。 related article, https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskscheduler.unobservedtaskexception?view=netframework-4.7.2 相关文章https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskscheduler.unobservedtaskexception?view=netframework-4.7.2

  2. new a thread in my code, without any specific configuration can also, any unhandled exception inside the thread can crash the main process. 在我的代码中新建一个线程,没有任何特定的配置也可以,线程内部任何未处理的异常都可能使主进程崩溃。 related article, https://docs.microsoft.com/en-us/dotnet/standard/threading/exceptions-in-managed-threads 相关文章, https://docs.microsoft.com/zh-cn/dotnet/standard/threading/exceptions-in-managed-threads

two attributes of one thread: {isbackground, isThreadPoolThread}. 一个线程的两个属性:{isbackground,isThreadPoolThread}。

  1. task is {true, true} 任务是{true,true}
  2. artificial thread is {true, false}, 人工线程为{true,false},
  3. subscriber callback thread is {true, false}. 订户回调线程为{true,false}。

is there any other configuration, like , can set to control whether or not to crash the main process? 是否还有其他配置(例如)可以设置为控制是否使主进程崩溃?

You can handle AppDomain.CurrentDomain.UnhandledException event in which you can get the current process by using Process.GetCurrentProcess() and kill that process so your exe will terminate. 您可以处理AppDomain.CurrentDomain.UnhandledException事件,在该事件中,您可以使用Process.GetCurrentProcess()获取当前进程并杀死该进程,以便您的exe终止。

Put this line Main() in Program.cs : 将此行Main()放在Program.cs

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException

Then implement CurrentDomain_UnhandledException as below. 然后,如下所示实现CurrentDomain_UnhandledException

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    var currentProcess = Process.GetCurrentProcess();
    currentProcess.Kill();
}

This is just concept as per your problem statement. 根据您的问题陈述,这只是概念。 You can change it as per your need. 您可以根据需要进行更改。

Any exception inside a COM was wrapped as a HRESULT return to it's client by RCW. RCW将COM内的任何异常包装为HRESULT返回给其客户端。 So, no way to crash the main process when the exception was thrown inside a COM thread. 因此,当在COM线程中引发异常时,无法使主进程崩溃。

The answer may be related with: COM methods report errors by returning HRESULTs; 答案可能与以下有关: COM方法通过返回HRESULT报告错误; .NET methods report them by throwing exceptions. .NET方法通过抛出异常来报告它们。 The runtime handles the transition between the two. 运行时处理两者之间的过渡。 Each exception class in the .NET Framework maps to an HRESULT. .NET Framework中的每个异常类都映射到一个HRESULT。

from https://docs.microsoft.com/en-us/dotnet/framework/interop/how-to-map-hresults-and-exceptions 来自https://docs.microsoft.com/zh-cn/dotnet/framework/interop/how-to-map-hresults-and-exceptions

and other helpful links: https://docs.microsoft.com/en-us/dotnet/framework/interop/runtime-callable-wrapper 和其他有用的链接: https : //docs.microsoft.com/zh-cn/dotnet/framework/interop/runtime-callable-wrapper

https://docs.microsoft.com/en-us/dotnet/framework/interop/com-callable-wrapper https://docs.microsoft.com/en-us/dotnet/framework/interop/com-callable-wrapper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM