using System;
using System.Threading.Tasks;
class Example
{
static void Main()
{
int result = GetMultipleAsync(6).Result;
Console.WriteLine($"The returned value is {result:N0}");
}
static async Task‹int› GetMultipleAsync(int secondsDelay)
{
Console.WriteLine("Executing GetMultipleAsync...");
if (secondsDelay < 0 || secondsDelay > 5)
throw new ArgumentOutOfRangeException("secondsDelay cannot exceed 5.");
await Task.Delay(secondsDelay * 1000);
return secondsDelay * new Random().Next(2,10);
}
}
// The example displays the following output:
// Executing GetMultipleAsync...
//
// Unhandled Exception: System.AggregateException:
// One or more errors occurred. (Specified argument was out of the range of valid values.
// Parameter name: secondsDelay cannot exceed 5.) --->
// System.ArgumentOutOfRangeException: Specified argument was
// out of the range of valid values.
// Parameter name: secondsDelay cannot exceed 5.
// at Example.‹GetMultiple›d__1.MoveNext() in Program.cs:line 17
// --- End of inner exception stack trace ---
// at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
// at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
// at Example.Main() in
//C:\Users\ronpet\Documents\Visual Studio 2017\Projects\local-functions\async1\Program.cs:line 8