C#winforms application.threadexception没有第二次触发 [英] C# winforms application.threadexception not firing second time

查看:87
本文介绍了C#winforms application.threadexception没有第二次触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在尝试捕获递归异常时遇到问题,希望有人可以提供帮助。

这是我的Program.cs代码,配置为处理例外。

Hi All,
I am having an issue while trying to catch a recursive exception, hope someone can help.
Here is my Program.cs code configured to handle exceptions.

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += Application_ThreadException;

            Application.Run(new Form1());
        }



这是用例。

1.用户将加载Form1,我希望可能会有一些异常由于数据加载错误。


Here is the use case.
1. User will load the Form1 and there i am expecting some exception might come due to data load errors.

private void Form1_Load(object sender, EventArgs e)
{throw new Exception("Data Load Error");}





2.然后它会点击



2. Then It will hit

Application_ThreadException;

事件我们正试图关闭表格并重新加载。



event and there we are trying to Close the form and Reload it Once again.

 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
             Form currentForm = GetActiveMainForm();
            if (currentForm != null)
            {
string formToOpen = currentForm.Name;
currentForm.BeginInvoke(new MethodInvoker(currentForm.Close));
                object form = Assembly.GetEntryAssembly().CreateInstance("ExceptionHandling." + formToOpen);
                (form as Form).Show();
}



}



到目前为止一切正常。现在

3.当我重新加载表单时,它应该抛出相同的异常并且


}

So far it works fine. Now
3.When i ReLoad the Form, it should throw the same exception and

Application_ThreadException

应该触发并且此过程应该是

递归发生。

First Load => Error => CloseAndReLoad =>错误=> CloseAndReload =>错误.. 等等..

should fire and this process supposed to be
happen recursively.
First Load=>Error=>CloseAndReLoad=>Error=>CloseAndReload=>Error.. so on..

HERE , I am trying to check if data load fails 3 times , then close the Module/Form



但是当它第二次加载表单(在第2步之后)并抛出异常时,它不会转到


But when it Loads the Form 2nd time(after step 2) and throws the exception, it is not going to

Application_ThreadException

事件处理程序。



我试图弄清楚这种行为的根本原因,但不能。



希望有人会在这里说清楚,如果我遗漏了什么或这是默认的预期行为吗?



提前谢谢,

Prathap < br $> b $ b

我尝试了什么:



我已经尝试过设置

event handler.

I am trying to figure out the root cause for this behaviour, but couldn't.

Hope somebody will shed some light here, if am missing anything or is this the default expected behavior?

Thanks in advance,
Prathap

What I have tried:

I have already tried setting

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);



处理以递归方式抛出异常(使用AppDomain.CurrentDomain.UnhandledException事件),但即使在处理异常后,它也不会退出并继续抛出相同的异常。


which handles to throw the exception(using AppDomain.CurrentDomain.UnhandledException event) recursively, but even after handling exception, it wont exit and keep on throwing the same exception.

推荐答案

这可能会有所帮助: ThreadExceptionEventArgs Class(System.Threading) [ ^ ]
This may help: ThreadExceptionEventArgs Class (System.Threading)[^]


这篇关于C#winforms application.threadexception没有第二次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆