我有一个IEnumerable序列,其中包含一些阻塞网络操作(在下面的示例代码中替换为一些简单的产量)。我正在使用Reactive Extensions将来自网络的数据流转换为可观察的序列。
我正在寻找一种方法来将异常编组到主线程中,以便未处理的异常不会导致我的应用程序终止。我不能在IEnumerable线程上放置try / catch块,因为编译器不允许在try / catch语句中使用yield return语句。
using System;
using System.Collections.Generic;
using System.Concurrency;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Main thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
var observable = TestEnumerable().ToObservable(Scheduler.NewThread); //Needs to be on a new thread because it contains long-running blocking operations
// Use subject because we need many subscriptions to a single data source
var subject = new Subject<int>();
subject.Subscribe(x => Console.WriteLine("Subscriber1: " + x + " on thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId),
x => Console.WriteLine("Subscriber1 ERROR: " + x+ " on thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId),
() => Console.WriteLine("Subscriber1 Finished"+ " on thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId));
subject.Subscribe(x => Console.WriteLine("Subscriber2: " + x + " on thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId),
x => Console.WriteLine("Subscriber2 ERROR: " + x+ " on thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId),
() => Console.WriteLine("Subscriber2 Finished"+ " on thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId));
Console.WriteLine("Press key to start receiving data");
Console.ReadKey();
var sub = observable.Subscribe(subject);
Console.WriteLine("Press key to exit");
Console.ReadKey();
sub.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("Caught exception on main thread");
}
}
public static IEnumerable<int> TestEnumerable()
{
while (true)
{
yield return 1;
Thread.Sleep(200);
yield return 2;
Thread.Sleep(200);
yield return 3;
Thread.Sleep(200);
throw new InvalidOperationException();
}
}
}
}
答案 0 :(得分:3)
解决方案取决于您是否可以使用Dispatcher / SynchronisationContext。在这种情况下,最好使用一个。
解决方案1:Dispatcher / SynchronisationContext可用
(即使用WPF,Windows窗体或自定义Dispatcher循环)
您可以使用ObserveOn
+ Catch
将错误移回Dispatcher线程。我已经看到在WPF应用程序中使用它并且它运行良好。
您如何移动IScheduler
/ DispatcherScheduler
取决于您(我们使用IoC)
public static IObservable<T> CatchOn<T>(this IObservable<T> source,
IScheduler scheduler)
{
return source.Catch<T,Exception>(ex =>
Observable.Throw<T>(ex).ObserveOn(scheduler));
}
// We didn't use it, but this overload could useful if the dispatcher is
// known at the time of execution, since it's an optimised path
public static IObservable<T> CatchOn<T>(this IObservable<T> source,
DispatcherScheduler scheduler)
{
return source.Catch<T,Exception>(ex =>
Observable.Throw<T>(ex).ObserveOn(scheduler));
}
解决方案2:没有可用的调度程序
不使用Console.ReadKey()
,而是使用ManualResetEvent
并等待它,然后抛出可变错误:
static void Main(string[] args)
{
try
{
Console.WriteLine("Main thread: " + System.Threading.Thread.CurrentThread.ManagedThreadId);
var observable = TestEnumerable().ToObservable(Scheduler.NewThread); //Needs to be on a new thread because it contains long-running blocking operations
// Use subject because we need many subscriptions to a single data source
var subject = new Subject<int>();
Exception exception = null;
ManualResetEvent mre = new ManualResetEvent(false);
using(subject.Subscribe(
x => Console.WriteLine(x),
ex => { exception = ex; mre.Set(); },
() => Console.WriteLine("Subscriber2 Finished")))
using(subject.Subscribe(
x => Console.WriteLine(x),
ex => { exception = ex; mre.Set(); },
() => Console.WriteLine("Subscriber2 Finished")))
using (observable.Subscribe(subject))
{
mre.WaitOne();
}
if (exception != null)
throw exception;
}
catch (Exception ex)
{
Console.WriteLine("Caught exception on main thread");
}
}