Пример #1
0
        /// <summary>
        /// Test the asynchronous processor using random delays.
        /// </summary>
        /// <param name="maintainOrder"></param>
        private static void TestDemonstration(bool maintainOrder)
        {
            var processor      = new AsyncProcessor <InputPacket, OutputPacket>(ProcessPacketAsync, maintainOrder);
            var resultObserver = new ResultObserver();

            using (var resultObserverSubscription = processor.Subscribe(resultObserver)) {
                for (int i = 0; i < 8; i++)
                {
                    var packet = new InputPacket(i);
                    Console.WriteLine(packet);
                    processor.OnNext(packet);

                    Thread.Sleep(Random.Next(50, 150));
                }
                processor.OnCompleted();

                while (!resultObserver.Completed)
                {
                }
            }
        }
Пример #2
0
        /// <summary>
        /// The process function for the asynchronous processor to use for each input.
        /// </summary>
        /// <param name="packet">The input packet.</param>
        /// <returns>Task to return the processed input packet as an output packet.</returns>
        private static async Task <OutputPacket> ProcessPacketAsync(InputPacket packet)
        {
            await Task.Delay(Random.Next(250, 500));

            return(new OutputPacket(packet.Index));
        }