STA线程异常传播
我有一个必须作为 STA 运行的函数,并且我想将其异常传播到调用线程。在这里:
public void ExceptionBePropagatedThroughHere()
{
Thread thread = new Thread(TheSTAThread);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
public void MainFunction()
{
try
{
ExceptionBePropagatedThroughHere();
}
catch(Exception e)
{
//will not hit here
}
}
将 STA 属性放在“MainFunction”上不是一个选项。 我注意到如果我使用任务,尝试捕获任务连接会将异常传播到调用线程,但是我无法指定以 STA 方式运行任务。
问题是如何将作为 STA 运行的异常传播到上例中的“MainFunction”?
提前致谢。
I have a function that must be running as STA, and I want to propagate its exceptions to calling thread. Here it is:
public void ExceptionBePropagatedThroughHere()
{
Thread thread = new Thread(TheSTAThread);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
public void MainFunction()
{
try
{
ExceptionBePropagatedThroughHere();
}
catch(Exception e)
{
//will not hit here
}
}
Putting STA attribute on "MainFunction" is not an option here.
I noticed if I was using Task, try catch on task join will propagate exception to calling thread, however I cannot specify run a task as STA.
The question is how to propagate exception running as STA to "MainFunction" in the example ablove?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遵循了汉斯的建议,解决方案如下所示,不需要触发任何事件。
I followed Hans' suggestion and the solution looks like below, no events need to be fired.
在等待期间,您需要保持消息泵的启动和运行。 Thread.Join 不是您可以使用的选项,因为它会阻塞您的线程,直到另一个线程终止。
一个非常简单的消息泵是创建两个名为 StaThreadExited 和 StaThreadExceptionEvent 的事件。您可以对事件数组使用 WaitHandle.WaitAny。如果这是一个异常事件,您可以从共享变量中获取它,然后将其重新抛出到您自己的线程中。我什至前段时间在这里发布了 代码 。
你的,
阿洛伊斯·克劳斯
You need to keep a message pump up and running while you are waiting. A Thread.Join is not the option you can use since it will block your thread until the other thread terminates.
A very simple message pump is that you create two events called StaThreadExited and StaThreadExceptionEvent. The you can use a WaitHandle.WaitAny on an array of events. If it was an exception event you can fetch it from e.g. a shared variable and rethrow it in your own thread. I have even posted the code here some time ago.
Yours,
Alois Kraus
线程是 STA 还是 MTA 并不重要。其中未捕获的异常将使您的应用程序崩溃。您不能将其扔到创建行为不当线程的线程中,除非您设计一种手动执行此操作的方法:例如捕获、将其存储在某处并通知主线程出现错误
doesn't matter if the thread is STA or MTA. An uncaught exception in it will crash your application. You can't throw it to the thread that created your misbehaving thread unless you devise a method to do so manually: like catching, storing it somewhere, and notifying the main thread that there was an error
这是一个老问题,但我想我会尝试改进建议的解决方案。 C# 已经具有在线程之间传输异常和结果的便捷机制。它称为
任务
。如果您需要 STA 线程,则无法执行Task.Run
,但是使用这样的帮助程序,您可以使用 STA 线程获得同样的便利:当然,可以通过添加混合通用返回类型。
This is an old question but I thought I would try to improve on the suggested solution. C# already has a convenient mechanism for transferring exceptions and results between threads. It's called
Task
. You can't doTask.Run
if you need a STA thread, but with a helper such as this, you can have the same convenience with STA threads:Of course this could be further improved by adding the generic return type into the mix.