Created
January 24, 2020 06:33
-
-
Save wipiano/5cf89830ab75932526af328348464fff to your computer and use it in GitHub Desktop.
Task.WhenAll() が例外を throw しない (.NET Core SDK 3.1.101. windows 10)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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