0

I read the following code. What's Util.KeepRunning() in the code? It seems it's for waiting the async method to finish. But why it needs to be disposable?

var observable = Observable.Create<char>(async (observer, cancel) =>
{
    for (var i = 0; !cancel.IsCancellationRequested && i < 100; i++)
    {
        observer.OnNext(await GetCharAsync());
    }
});

//Here's how you can use it in LINQPad, for example:

// Create a disposable that keeps the query running.
// This is necessary, since the observable is 100% async.
var end = Util.KeepRunning();

observable.Subscribe(
    c => Console.WriteLine(c.ToString()),
    () => end.Dispose());
ca9163d9
  • 27,283
  • 64
  • 210
  • 413

0 Answers0