当创建例外做法的SynchronizationContext?做法、SynchronizationContext

2023-09-06 09:23:08 作者:奈何亦是泪

我创建的STA版本的的SynchronizationContext 在Windows工作流4.0的使用。我不知道该怎么办时,异常后,荷兰国际集团的回调。

I'm creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I'm wondering what to do about exceptions when Post-ing callbacks.

在的SynchronizationContext 的可用于发送(执行同步)或发布的类型(异步执行)代表的 SendOrPostCallback 。虽然在这两种情况下,我调用一个STA线程的代表,它很容易知道如何执行同步时处理异常。我阻塞调用线程,调用回调函数在我的工作线程,记录任何异常,疏通调用线程,并抛出任何异常记录的调用线程。

The SynchronizationContext can be used to Send (execute synchronously) or Post (execute asynchronously) delegates of type SendOrPostCallback. Although in both cases I invoke the delegate on a STA thread, its easy to know how to handle exceptions when executing synchronously. I block the calling thread, Invoke the callback on my worker thread, record any exceptions, unblock the calling thread, and throw any recorded exceptions on the calling thread.

我应该做的异步的发布目前还不太清楚。没有任何机制来传送从执行的线程回调用线程例外; 发表为100%,发射后不管。没有的的EndInvoke()或的WaitHandle 的在 SendOrPostCallback 。抛出将导致应用程序的任何异常被拆掉了。

What I should do on the asynchronous Post is less clear. There is no mechanism for transferring that exception from the executing thread back to the calling thread; Post is 100% fire and forget. There is no EndInvoke() or WaitHandle in the SendOrPostCallback. Any exceptions thrown will result in the application being torn down.

难道我没有选择,只能让扔在一个发表异常推倒我的应用程序?这似乎是在默认行为的 SynchronizationContexts 的框架中(谢谢你,反射镜)。我似乎无法弄清楚这是为什么。不应该有部分办法prevent异步的发布 - 从去热潮?

Do I have no choice but to let an exception thrown in a Post tear down my application? That seems to be the default behavior in the SynchronizationContexts in the framework (thank you, Reflector). I can't seem to figure out why this is. Shouldn't there be some way to prevent asynchronous Posts from going boom?

推荐答案

在这样的情况下我让替代原则指导我。我将执行相同的行为作为的SynchronizationContext 现有实例。如果不这样做违反了置换原则,可以回来咬你以意想不到的方式。

In situations like this I let the substitution principle guide me. I would implement the same behavior as the existing instances of SynchronizationContext. To do otherwise violates the substitution principle and could come back to bite you in unexpected ways.