Skip to content

Commit bd511e0

Browse files
committed
Added example for TaskWhenAllResult
1 parent 8a6769c commit bd511e0

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

TaskWhenAllResult/Program.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
var (intResult, stringResult, doubleResult) =
2+
await TaskHelper.StartWith(() => Task.FromResult(10))
3+
.And(() => Task.FromResult("Hello World"))
4+
.And(() => Task.FromResult(10d))
5+
.WaitAllAsync();
6+
7+
Console.WriteLine($"Int Result: {intResult}");
8+
Console.WriteLine($"String Result: {stringResult}");
9+
Console.WriteLine($"Double Result: {doubleResult}");
10+
11+
public static class TaskHelper
12+
{
13+
public static TaskHelper<T> StartWith<T>(Func<Task<T>> task)
14+
{
15+
return new TaskHelper<T>(task);
16+
}
17+
}
18+
19+
public class TaskHelper<T>
20+
{
21+
private readonly List<Func<Task<object>>> _tasks;
22+
23+
public TaskHelper(Func<Task<T>> initialTask)
24+
{
25+
_tasks = [() => initialTask().ContinueWith(t => (object)t.Result)];
26+
}
27+
28+
public TaskHelper<T, TNext> And<TNext>(Func<Task<TNext>> nextTask)
29+
{
30+
return new TaskHelper<T, TNext>(_tasks, nextTask);
31+
}
32+
33+
public async Task<T> WaitAllAsync()
34+
{
35+
var results = await Task.WhenAll(_tasks.Select(t => t()));
36+
return (T)results[0];
37+
}
38+
}
39+
40+
public class TaskHelper<T1, T2>
41+
{
42+
private readonly List<Func<Task<object>>> _tasks;
43+
44+
public TaskHelper(List<Func<Task<object>>> existingTasks, Func<Task<T2>> nextTask)
45+
{
46+
_tasks = [..existingTasks, () => nextTask().ContinueWith(t => (object)t.Result)];
47+
}
48+
49+
public TaskHelper<T1, T2, T3> And<T3>(Func<Task<T3>> nextTask)
50+
{
51+
return new TaskHelper<T1, T2, T3>(_tasks, nextTask);
52+
}
53+
54+
public async Task<(T1, T2)> WaitAllAsync()
55+
{
56+
var results = await Task.WhenAll(_tasks.Select(t => t()));
57+
return ((T1)results[0], (T2)results[1]);
58+
}
59+
}
60+
61+
public class TaskHelper<T1, T2, T3>
62+
{
63+
private readonly List<Func<Task<object>>> _tasks;
64+
65+
public TaskHelper(List<Func<Task<object>>> existingTasks, Func<Task<T3>> nextTask)
66+
{
67+
_tasks = [..existingTasks, () => nextTask().ContinueWith(t => (object)t.Result)];
68+
}
69+
70+
public async Task<(T1, T2, T3)> WaitAllAsync()
71+
{
72+
var results = await Task.WhenAll(_tasks.Select(t => t()));
73+
return ((T1)results[0], (T2)results[1], (T3)results[2]);
74+
}
75+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TaskWhenAllResult", "TaskWhenAllResult.csproj", "{BA2BABA4-B25F-4EAD-9312-D48595C58F64}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{BA2BABA4-B25F-4EAD-9312-D48595C58F64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{BA2BABA4-B25F-4EAD-9312-D48595C58F64}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{BA2BABA4-B25F-4EAD-9312-D48595C58F64}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{BA2BABA4-B25F-4EAD-9312-D48595C58F64}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

0 commit comments

Comments
 (0)