コード例 #1
0
ファイル: Program.cs プロジェクト: cash2one/HBNews
        static void Main()
        {
            FooWrapperProducer  obj      = new FooWrapperProducer();
            IDataProducer <Foo> producer = obj.Data;

            // count things *as they happen*
            // (this is a "future", not a value)
            var allCount = producer.Count();

            // some more interesting aggregates
            var justEven = from item in producer
                           where item.Bar % 2 == 0
                           select item;

            var evenCount  = justEven.Count();
            var oddCount   = producer.Count(x => x.Bar % 2 == 1);
            var evenAvgBar = justEven.Average(x => x.Bar);

            // or a more bespoke operation
            justEven.DataProduced += x =>
            {
                Console.WriteLine("Got a line: {0}", x.Bar);
            };

            // this could be loading an external file, etc
            using (Stream feed = CreateData())
            {
                Serializer.Merge(feed, obj);
            }

            // now we need to tell the listeners that
            // the feed is complete
            obj.Data.End();
            Console.WriteLine("All count: {0}", allCount.Value);
            Console.WriteLine("Even count: {0}", evenCount.Value);
            Console.WriteLine("Odd count: {0}", oddCount.Value);
            Console.WriteLine("Even average Bar: {0}", evenAvgBar.Value);
            // (with later versions don't need .Value)
        }