在全局异常处理程序中处理线程异常?

发布于 2024-09-17 09:02:19 字数 340 浏览 4 评论 0原文

我有一个安装了全局异常处理程序的主应用程序。现在,对于在另一个线程中引发的某些特定异常,我希望调用全局异常处理程序。但它只处理主线程的异常。我还在线程内尝试了以下操作,但它也不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我喜欢麦丽素 2024-09-24 09:02:19

怎么样:向主线程发送一条应该引发异常的消息。

How about this: send a message to the main thread which exception should be raised.

空城旧梦 2024-09-24 09:02:19

我在线程中处理了异常,将消息保存为公共字符串变量。当线程的状态发生变化时,我检查了主线程中的变量。使得代码非常可读。

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.

笙痞 2024-09-24 09:02:19

如果您有兴趣,我会在我的框架中执行此操作(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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文