コード例 #1
0
        public async Task AsyncHandler()
        {
            #region Snippet:Azure_Core_Samples_EventSamples_AsyncHandler
            var client = new AlarmClient();
            client.Ring += async(SyncAsyncEventArgs e) =>
            {
                await Console.Out.WriteLineAsync("Wake up!");
            };

            await client.SnoozeAsync();

            #endregion
        }
コード例 #2
0
        public void SyncHandler()
        {
            #region Snippet:Azure_Core_Samples_EventSamples_SyncHandler
            var client = new AlarmClient();
            client.Ring += (SyncAsyncEventArgs e) =>
            {
                Console.WriteLine("Wake up!");
                return(Task.CompletedTask);
            };

            client.Snooze();
            #endregion
        }
コード例 #3
0
        public void Exceptions()
        {
            #region Snippet:Azure_Core_Samples_EventSamples_Exceptions
            var client = new AlarmClient();
            client.Ring += (SyncAsyncEventArgs e) =>
                           throw new InvalidOperationException("Alarm unplugged.");

            try
            {
                client.Snooze();
            }
            catch (AggregateException ex)
            {
                ex.Handle(e => e is InvalidOperationException);
                Console.WriteLine("Please switch to your backup alarm.");
            }
            #endregion
        }
コード例 #4
0
        public async Task CombinedHandler()
        {
            #region Snippet:Azure_Core_Samples_EventSamples_CombinedHandler
            var client = new AlarmClient();
            client.Ring += async(SyncAsyncEventArgs e) =>
            {
                if (e.IsRunningSynchronously)
                {
                    Console.WriteLine("Wake up!");
                }
                else
                {
                    await Console.Out.WriteLineAsync("Wake up!");
                }
            };

            client.Snooze();            // sync call that blocks
            await client.SnoozeAsync(); // async call that doesn't block

            #endregion
        }