Skip to content

Instantly share code, notes, and snippets.

@wipiano
Created January 24, 2020 06:33
Show Gist options
  • Save wipiano/5cf89830ab75932526af328348464fff to your computer and use it in GitHub Desktop.
Save wipiano/5cf89830ab75932526af328348464fff to your computer and use it in GitHub Desktop.
Task.WhenAll() が例外を throw しない (.NET Core SDK 3.1.101. windows 10)
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
// does not throw exception
await WhenAll1Async();
}
static async Task WhenAll1Async()
{
await Task.Yield();
var tasks = new List<Task>();
for (var i = 0; i < 4; i++)
{
tasks.Add(RunAsync());
}
tasks.Add(ThrowExceptionAsync());
await Task.WhenAll(tasks);
}
/// <summary>
/// throws exception after 10 sec
/// </summary>
static async Task ThrowExceptionAsync()
{
await Task.Yield();
await Task.Delay(10000);
Console.WriteLine("throw");
throw new Exception();
}
static async Task RunAsync()
{
await Task.Yield();
while (true)
{
await Task.Delay(1000);
Console.WriteLine("running");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment