本文介绍了无扩展燕子从OnNext()呼吁一个线程池线程异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的Rx 2 .NET 4.5。当下面的代码运行时,它只是静静地退出不执行OnCompleted委托或显示任何错误。如果我使用 Scheduler.CurrentThread ToObservable ,它至少会抛出错误并终止程序,在这一点不执行OnCompleted有道理。但是,当这不是主要的一个以外的线程中执行,这种行为似乎不合理的和不可接受的。难道我错过了什么?



 静态无效的主要()
{
Enumerable.Range(0,1 )
.ToObservable(Scheduler.Default)
.Subscribe(O => {抛出新的异常(嗒嗒);},()=> Console.WriteLine(已完成));

Thread.sleep代码(2000);
}



编辑:
是的,当运行作为控制台应用程序,它总是会抛出错误,无论什么线程观察执行上。



然而,当我运行此代码在NUnit的测试作为如下,它2秒(线程睡眠时间)没有任何错误或消息(预期已完成)后自行退出。因此,它实际上是NUnit的导致该问题的?



  [的TestFixture] 
类节目
{
〔测试]
公共无效测试()
{
Enumerable.Range(0,1)
.ToObservable(Scheduler.Default)
.Subscribe($ b $博=> {抛出新的异常(嗒嗒);}
()=> Console.WriteLine(已完成));
Thread.sleep代码(2000);
}
}


解决方案

接收不赶观察员抛出的异常。这是一个已经长之前已经讨论了很重要的设计原则,但由于某种原因,它只是作为一些的6.4节中的



Essentially, this guideline ensures that, from the perspective of an observer, OnError will only be called by exceptions originating from the observable itself, including any calls to user code that participate directly in the computation (rather than merely observing the results). If this were not the case, then an observer may not be able to distinguish whether an exception passed to OnError is a bug in their OnNext handler or perhaps a bug in the observable.

But more importantly, it also ensures that any exception thrown by an OnNext handler goes unhandled. This makes it easier to debug your program and protects user data.

That being said, the reason why you may be observing different behavior when OnNext is executed on a pooled thread is simply a consequence of your debugging experience. Try enabling first-chance exceptions.

Furthermore, I'd also avoid the race condition by changing Thread.Sleep to Console.ReadKey().

这篇关于无扩展燕子从OnNext()呼吁一个线程池线程异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:45