Exemplo n.º 1
0
        private static async Task SimpleAsyncStreamAsync()
        {
            var aDevice = new ADevice();

            await foreach (var x in aDevice.GetSensorData())
            {
                Console.WriteLine($"{x.Value1} {x.Value2}");
            }
        }
Exemplo n.º 2
0
        private static async Task UseEnumeratorAsync()
        {
            var aDevice = new ADevice();

            await foreach (var x in aDevice.GetSensorData1())
            {
                Console.WriteLine($"{x.Value1} {x.Value2}");
            }
        }
Exemplo n.º 3
0
        static async Task Main()
        {
            var aDevice = new ADevice();

            await foreach (var x in aDevice.GetSensorData())
            {
                Console.WriteLine($"{x.Value1} {x.Value2}");
            }
        }
Exemplo n.º 4
0
        private static async Task WhileLoopAsync()
        {
            var aDevice = new ADevice();

            IAsyncEnumerable <SensorData> dataStream = aDevice.GetSensorData1();

            await using IAsyncEnumerator <SensorData> enumerator = dataStream.GetAsyncEnumerator();
            while (await enumerator.MoveNextAsync())
            {
                var sensorData = enumerator.Current;
                Console.WriteLine($"{sensorData.Value1} {sensorData.Value2}");
            }
        }
Exemplo n.º 5
0
        private static async Task WithCancellationAsync()
        {
            try
            {
                var cts = new CancellationTokenSource();
                cts.CancelAfter(5000);
                var aDevice = new ADevice();

                await foreach (var x in aDevice.GetSensorData2().WithCancellation(cts.Token))
                {
                    Console.WriteLine($"{x.Value1} {x.Value2}");
                }
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 6
0
        static async Task Main()
        {
            var aDevice = new ADevice();

            await foreach (var x in aDevice.GetSensorData1())
            {
                Console.WriteLine($"{x.Value1} {x.Value2}");
            }

            //var cts = new CancellationTokenSource();
            //cts.CancelAfter(5000);
            //var aDevice = new ADevice();
            //await foreach(var x in aDevice.GetSensorData(cts.Token))
            //{
            //    Console.WriteLine($"{x.Value1} {x.Value2}");
            //}
            Console.WriteLine("finished");
        }
Exemplo n.º 7
0
        private static async Task AsyncStreamWithCancellation()
        {
            var cts = new CancellationTokenSource();

            cts.Token.Register(() => Console.WriteLine("cancellation requested"));
            cts.CancelAfter(5000);

            try
            {
                var aDevice = new ADevice();
                await foreach (var x in aDevice.GetSensorData1(cts.Token)) //.WithCancellation(cts.Token))
                {
                    Console.WriteLine($"{x.Value1} {x.Value2}");
                }
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 8
0
        private static async Task Demo2Async()
        {
            var aDevice = new ADevice();

            IAsyncEnumerable <SensorData> en         = aDevice.GetSensorData1();
            IAsyncEnumerator <SensorData> enumerator = en.GetAsyncEnumerator();

            try
            {
                while (await enumerator.MoveNextAsync())
                {
                    var sensorData = enumerator.Current;
                    Console.WriteLine($"{sensorData.Value1} {sensorData.Value2}");
                }
            }
            finally
            {
                await enumerator.DisposeAsync();
            }
        }