为什么下面的代码不起作用?
var observable = Observable.Create<int>(o => new Action(() =>
{
o.OnNext(0);
o.OnCompleted();
}));
foreach (var item in observable.ToEnumerable()) // <-- Here blocks
{
// Never get here
}
以下内容也不起作用:
Task.Run(() =>
{
foreach (var item in observable.ToEnumerable()) // <-- Still blocks
{
// Never get here
}
}).Wait();
有趣的是,Generate 方法按预期工作:
var observable = Observable.Generate(0, i => i < 1, i => i + 1, i => i);
foreach (var item in observable.ToEnumerable())
{
// Is ok
}