AsyncObservables.Program.SearchingWithCancellation C# (CSharp) Method

SearchingWithCancellation() public static method

public static SearchingWithCancellation ( ) : void
return void
        public static void SearchingWithCancellation()
        {
            Demo.DisplayHeader("Creating async observable with async-await and cancellation");

            var exampleResetEvent = new AutoResetEvent(false);

            // Change the index to when you want the subscription disposed
            int cancelIndex = 1;

            var results = SearchEngineExample.Search_WithCancellation("Rx");

            IDisposable subscription = Disposable.Empty;
            subscription = results
                .Select((result, index) => new { result, index }) //adding the item index to the notification
                .Do(x =>
                {
                    if (x.index == cancelIndex)
                    {
                        Console.WriteLine("Cancelling on index {0}", cancelIndex);
                        subscription.Dispose();
                        exampleResetEvent.Set();
                    }
                })
                .Select(x => x.result) //rollback the observable to be IObservable<string>
                 .DoLast(() => exampleResetEvent.Set(), delay: TimeSpan.FromSeconds(1))
                .SubscribeConsole("results");

            exampleResetEvent.WaitOne();
        }