在全局异常处理程序中处理线程异常?
我有一个安装了全局异常处理程序的主应用程序。现在,对于在另一个线程中引发的某些特定异常,我希望调用全局异常处理程序。但它只处理主线程的异常。我还在线程内尝试了以下操作,但它也不起作用:
RunInMainThread (procedure
begin
raise EExceptionFromWithinThread.Create;
end);
其中 RunInMainThread 仅执行在主线程上下文中作为参数给出的匿名方法。
为什么这不起作用?处理这种情况的正确方法是什么?
I have a main application with a global exception handler installed. Now, for some specific exceptions being raised within another thread I want the global exception handler to be invoked. But it does only handle exceptions from the main thread. I also tried the following from within the thread but it does not work either:
RunInMainThread (procedure
begin
raise EExceptionFromWithinThread.Create;
end);
where RunInMainThread
just executes the anonymous method given as a parameter in the context of the main thread.
Why doesn't this work? What's the proper way to handle such a situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:向主线程发送一条应该引发异常的消息。
How about this: send a message to the main thread which exception should be raised.
我在线程中处理了异常,将消息保存为公共字符串变量。当线程的状态发生变化时,我检查了主线程中的变量。使得代码非常可读。
I handled the exception in the thread, saving the message as a common string variable. When the status of the thread changed, I checked the variable in the main thread. Makes for very readable code.
如果您有兴趣,我会在我的框架中执行此操作(http://www.csinnovations.com/framework_overview.htm< /a>)。
I do this in my framework if you are interested (http://www.csinnovations.com/framework_overview.htm).