0

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.

I expect any exception inside the subscriber to terminate the S.exe.

I did my investigation:

  1. task, with configuration can terminate the main process. related article, https://learn.microsoft.com/en-us/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://learn.microsoft.com/en-us/dotnet/standard/threading/exceptions-in-managed-threads

two attributes of one thread: {isbackground, isThreadPoolThread}.

  1. task is {true, true}
  2. artificial thread is {true, false},
  3. subscriber callback thread is {true, false}.

is there any other configuration, like , can set to control whether or not to crash the main process?

dhCompiler
  • 31
  • 4
  • You must do this programmatically so you can take care of the *other* essential detail. Super-duper important that you leave some kind of breadcrumb so the user can discover why the program terminated. You don't have a lot of options since you cannot control the user interface, System.Diagnostics.EventLog is all you really have. Then Environment.FailFast(). – Hans Passant Aug 21 '18 at 09:45

2 Answers2

0

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.

Put this line Main() in Program.cs:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException

Then implement CurrentDomain_UnhandledException as below.

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.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
  • Hi, Ramoliya, actually, this is my main function,[STAThread] static void Main() { AppDomain.CurrentDomain.UnhandledException += (sender, args) => { MessageBox.Show(args.ExceptionObject.ToString()); }; TaskScheduler.UnobservedTaskException += (sender, args) => { MessageBox.Show(args.ToString()); }; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1); } – dhCompiler Aug 21 '18 at 09:34
  • So you are already handling the `UnhandledException ` Just put process kill lines in that like this. AppDomain.CurrentDomain.UnhandledException += (sender, args) => { MessageBox.Show(args.ExceptionObject.ToString()); var currentProcess = Process.GetCurrentProcess(); currentProcess.Kill(); }; – Keyur Ramoliya Aug 21 '18 at 10:26
  • the problem is in my attached code above, I didn't got the message box. so, can I guess, it also can not kill my process using you proposed way? – dhCompiler Aug 21 '18 at 23:34
0

Any exception inside a COM was wrapped as a HRESULT return to it's client by RCW. So, no way to crash the main process when the exception was thrown inside a COM thread.

The answer may be related with: COM methods report errors by returning HRESULTs; .NET methods report them by throwing exceptions. The runtime handles the transition between the two. Each exception class in the .NET Framework maps to an HRESULT.

from https://learn.microsoft.com/en-us/dotnet/framework/interop/how-to-map-hresults-and-exceptions

and other helpful links: https://learn.microsoft.com/en-us/dotnet/framework/interop/runtime-callable-wrapper

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

dhCompiler
  • 31
  • 4