简体   繁体   English

捕获调用Application.DoEvents()时引发的异常

[英]Catching exceptions thrown when invoking Application.DoEvents()

Im new to .Net and I'm wondering why this code still raises unhandled exception. 我是.Net的新手,我想知道为什么此代码仍然引发未处理的异常。

try
{
    Application.DoEvents();
}
catch
{
}

Does anyone have any idea? 有人有什么主意吗? This code is invoked inside an event handler. 此代码在事件处理程序中调用。 It throws NullReferenceException. 它抛出NullReferenceException。 The funny thing is when I try to put a breakpoint right before this code, the exception is not being thrown. 有趣的是,当我尝试在此代码之前放置一个断点时,不会引发异常。 The exception occurs only when I let the code run continuously. 仅当我让代码连续运行时,才会发生异常。

Not all exceptions are catchable. 并非所有异常都是可捕获的。 You didn't tell us what exception you are seeing, but note that StackOverflowException and ExecutionEngineException can not ever ever ever be caught. 您没有告诉我们您正在看到什么异常,但是请注意,永远不会捕获StackOverflowExceptionExecutionEngineException I know the former can happen with Application.DoEvents (often in System.Drawing ) but I'm not sure about the latter. 我知道前者可能与Application.DoEvents (通常在System.Drawing )发生,但我不确定后者。

Anyway, your code is evil. 无论如何,您的代码是邪恶的。 Don't swallow exceptions. 不要吞下例外。 That means you are swallowing bugs. 这意味着您正在吞噬错误。

In general, try to avoid Application.DoEvents . 通常,请尝试避免Application.DoEvents There are really nasty reentrancy issues that can happen. 确实可能发生令人讨厌的重入问题。

Attention to use the Application.DoEvents() , most of the time is used for wrong things, like update a progress bar in a time spend process, wich can be done with asynchronous programming. 注意使用Application.DoEvents() ,大多数时间用于错误的事情,例如在时间花费过程中更新进度条,这可以通过异步编程来完成。 You can't catch all exceptions, please tell us what exception are raising. 您无法捕获所有异常,请告诉我们正在引发什么异常。

It could also be that when you are running the application without a breakpoint, the DoEvents is allowing another piece of code to execute and that piece of code is throwing the exception. 也可能是当您在没有断点的情况下运行应用程序时,DoEvents允许执行另一段代码,而段代码引发异常。

You should check the exception's StackTrace to see where it points you (or post the stack trace here and we can look at it). 您应该检查异常的StackTrace以查看其指向您的位置(或在此处发布堆栈跟踪,我们可以查看它)。 If this is the case you might not be seeing the exception when you're using a breakpoint and stepping because the DoEvents and threading don't fire the same ways. 如果是这种情况,则在使用断点并逐步执行时,您可能看不到异常,因为DoEvent和线程执行的方式不同。

If the exception is being thrown from elsewhere during the DoEvents that would also explain why you are getting an unhandled exception even though the above code is trying to black-hole any exceptions. 如果在DoEvents期间从其他地方抛出了异常,那么这也可以解释为什么您会得到未处理的异常,即使上面的代码试图对任何异常进行黑洞处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM