【发布时间】:2014-07-18 04:28:00
【问题描述】:
有人可以帮我对 IObserver 进行同步订阅,这样调用方法就会阻塞,直到订阅完成。 例如:
出版商
public static class Publisher {
public static IObservable<string> NonBlocking()
{
return Observable.Create<string>(
observable =>
{
Task.Run(() =>
{
observable.OnNext("a");
Thread.Sleep(1000);
observable.OnNext("b");
Thread.Sleep(1000);
observable.OnCompleted();
Thread.Sleep(1000);
});
return Disposable.Create(() => Console.WriteLine("Observer has unsubscribed"));
});
}
}
订阅者
public static class Subscriber{
public static bool Subscribe()
{
Publisher.NonBlocking().Subscribe((s) =>
{
Debug.WriteLine(s);
}, () =>
{
Debug.WriteLine("Complete");
});
// This will currently return true before the subscription is complete
// I want to block and not Return until the Subscriber is Complete
return true;
}
}
【问题讨论】:
标签: .net system.reactive reactive-programming