c#:Stack在Invoke停止

时间:2014-02-17 14:47:46

标签: c# tcp controls invoke

在C#/ .net中,当发生错误时,堆栈将记录在Windows错误日志中。但是,我的应用程序有很多跨线程调用。堆栈跟踪似乎在每个Invoke(MarshalledInvoke)停止。

例如:

我的筹码:

Exception Info: Facebook.FacebookApiException
Stack:
at System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
at System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
at Test.TcpTest.InterpretData()
at Test.TcpTest.Incoming(System.String)
at Test.TcpTest.cLoop()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

在堆栈中进一步执行的代码中发生异常。但是,堆栈每次都停在marshalledInvoke上。

我有什么办法可以继续调用堆栈吗?

1 个答案:

答案 0 :(得分:5)

当您使用Control.Invoke()而不是Control.BeginInvoke()时,UI线程上引发的任何异常都会被封送回您的工作线程并重新抛出。有一些信息丢失,你得到最内部的例外。

使用Invoke()很少是正确的,并且通常有可能导致死锁,混淆异常和延迟工作线程。在UI线程执行委托目标之前,哪个被阻止。这可能需要一段时间。确保你确实需要它。如果您调用需要其返回值的方法,那么这才是真正必要的。应该始终避免这种情况。

您可以使用Debug + Exceptions调试这些异常,勾选CLR Exceptions的Thrown复选框。调试器现在将停止在抛出异常的代码处。