﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using System.Concurrency;

namespace ConsoleApplication2
{
    class Program
    {
        static List<int> listCollection = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        public static IEnumerable<int> yieldReturnExplode()
        {
            int i = 0;
            while (true)
            {
                System.Threading.Thread.Sleep(500);
                if (i % 10 == 9)
                {
                    i++;
                    throw new Exception("Bang");
                }
                yield return i;
                i++;
            }
        }

        public class MyObserver : IObserver<int>
        {
            public void OnCompleted()
            {
                Console.WriteLine("Completed");
            }

            public void OnError(Exception error)
            {
                Console.WriteLine("Error: " + error.ToString());
            }

            public void OnNext(int value)
            {
                Console.WriteLine("Item: " + value);
            }
        }


        [STAThread()]
        static void Main(string[] args)
        {

            var obsList = listCollection.ToObservable();
            obsList.Subscribe(new MyObserver());



















            ////Subscribe Again
            //obsList.Subscribe(new MyObserver());






















            ////Subscribe without Observer implementation
            //obsList.Subscribe(
            //    x => Console.WriteLine("item " + x),
            //    ex => Console.WriteLine("Error: " + ex.ToString()),
            //    () => Console.WriteLine("Completed")
            //        );






















            ////See error handling (Run, don't debug)
            //yieldReturnExplode().ToObservable().Subscribe(new MyObserver());







            Console.WriteLine("\r\n\r\nFinished Demo");
            Console.ReadLine();

        }

    }





}
