Exemplo n.º 1
0
        private static async Task Demo1Async()
        {
            var sensorDevice = new SensorDevice();

            await foreach (var x in sensorDevice.GetSensorData1())
            {
                Console.WriteLine($"{x.Value1} {x.Value2}");
            }
        }
Exemplo n.º 2
0
        private static async Task Demo2Async()
        {
            var sensorDevice = new SensorDevice();

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

            while (await enumerator.MoveNextAsync())
            {
                var sensorData = enumerator.Current;
                Console.WriteLine($"{sensorData.Value1} {sensorData.Value2}");
            }
            await enumerator.DisposeAsync();
        }
Exemplo n.º 3
0
        private static async Task Demo3Async()
        {
            try
            {
                var cts = new CancellationTokenSource();
                cts.CancelAfter(5000);
                var sensorDevice = new SensorDevice();

                await foreach (var x in sensorDevice.GetSensorData2(cts.Token))
                {
                    Console.WriteLine($"{x.Value1} {x.Value2}");
                }
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }