我注意到Windows窗体场景中涉及线程和UI的一些奇怪行为,所以,当然,这意味着要使用InvokeRequired属性。情况:我的应用程序使用一个线程来完成一些工作,线程向UI发送一个事件。用户界面显示基于国际化系统的消息,该系统由带有密钥的字典组成。 I18N系统无法在字典中找到密钥并崩溃。InvokeRequired异常处理
说明:应用程序处于调试模式,我有一个尝试赶上整个“Application.Run();”回到Program.cs中。然而,那个try-catch没有被达到,因为我在这里要讨论的是基于内部异常处理,但我提到它以防万一。
所以现在这里来了有趣的部分:
为什么,我的生活中,确实Visual Studio的“检查员”异常从我的信息?在下面的代码中,你会看到if(InvokeRequired)分支,一个try-catch。我记录异常。 ex.InnerException是NULL,提供的ex.StackTrace是贫血的(只有1步)。现在,如果我尝试捕获并简单地让它通过调试器崩溃,我会得到一个非常宽泛的栈跟踪。这是为什么?
更糟糕的是,两个堆栈跟踪版本中的既没有也没有包含有关i18N崩溃的任何信息。他们只是说“给定的钥匙不在字典中。”并给我一个堆栈跟踪到Invoke声明。
在其他分支(即InvokeRequired ==假),如果我把一个try-catch,我可以成功地捕捉我的异常给国际化系统。正如你所看到的,我试图用InnerException将异常发送回InvokeRequired == true分支。然而,即使如此,InnerException在那里保持NULL,我无法访问我的i18N错误。
我对所有这些事情感到困惑,也许有人可以帮助摆脱这里的一些光。如果你有非常强大的灯笼。
这里是函数的代码。
private delegate void AddMessageToConsole_DELEGATE (frmMainPresenter.PresenterMessages message);
private void AddMessageToConsole (frmMainPresenter.PresenterMessages message)
{
if (InvokeRequired)
{ //Catching any errors that occur inside the invoked function.
try { Invoke(new AddMessageToConsole_DELEGATE(AddMessageToConsole), message); }
catch (Exception ex) { MSASession.ErrorLogger.Log(ex); }
//Invoke(new AddMessageToConsole_DELEGATE(AddMessageToConsole), message);
}
else
{
string message_text = ""; //Message that will be displayed in the Console/written in the Log.
try
{
message_text = I18N.GetTranslatedText(message)
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
txtConsole.AppendText(message_text);
}
}
#2有点混乱。这两者都不意味着这两个例外都会提供关于i18N崩溃的信息。我想你的意思是说,没有任何例外给出这些信息。你可能想重新说一遍。 对不起,如果我是一个语法纳粹:P – 2010-08-20 10:34:46
真:: :: D.我更新,对不起! – Axonn 2010-08-20 10:47:38
有没有什么办法可以让我的问题更好,让更多的人看到它? :: - >。 – Axonn 2010-08-20 13:08:08