Anyone knows how to prevent app to be terminated when unhandled exception occurred from another thread? Seems doing AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException
is only to capture when the error occurred but it won't handle it -- I mean it won't clear the error so the error keep continues.
Here's my workaround :
using System;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
class ErrorForm : Form {
private Button button2;
Thread newThread = null;
private ErrorForm() {
InitializeComponent();
}
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.Run(new ErrorForm());
}
// Start a new thread, separate from Windows Forms, that will throw an exception.
private void button2_Click(object sender, EventArgs e) {
newThread = new Thread(() => {
throw new Exception("Aha!");
});
newThread.Start();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
MessageBox.Show("CurrentDomain_UnhandledException");
}
private void InitializeComponent() {
this.button2 = new Button();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(12, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(260, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Make Error";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += this.button2_Click;
//
// ErrorHandlerForm
//
this.ClientSize = new System.Drawing.Size(284, 52);
this.Controls.Add(this.button2);
this.Name = "ErrorHandlerForm";
this.ResumeLayout(false);
}
}
}
When I run this code and press button2, then CurrentDomain_UnhandledException
invoked as expected, but next it goes back again to throw new Exception("Aha!");
. The best thing I can do is to kill the process in method CurrentDomain_UnhandledException
like this:
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
MessageBox.Show("CurrentDomain_UnhandledException");
if (e.IsTerminating) {
Process.GetCurrentProcess().Kill();
}
}
But it's not acceptable in my scenario of app. I want to the app (main form) kept live and this error smoothly handled. And I don't want to replace my logic done by Thread
with BackgroundWorker
. Is that possible? Any good solution for this?
Thank you in advance!