Last active
May 4, 2020 04:54
-
-
Save programmation/b3da676aa26b81811b707555a49e9659 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Linq; | |
using System.Threading.Tasks; | |
namespace EventActionFunc | |
{ | |
public class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
var test = new Testclass(); | |
await test.AsyncTestMethod(); | |
} | |
} | |
public class Testclass | |
{ | |
Action<bool> MyAction { get; set; } | |
Func<Task<bool>> MyAwaitedAction { get; set; } | |
public async Task AsyncTestMethod() | |
{ | |
Log("Setup"); | |
for (int index = 0; index < 3; index++) | |
{ | |
var capturedIndex = index; | |
MyAction += (result) => | |
{ | |
Log($"{result} ({capturedIndex})"); | |
}; | |
} | |
MyAwaitedAction += async () => | |
{ | |
Log("Action 1 starting"); | |
await Task.Delay(3000); | |
Log($"Action 1 finished"); | |
return true; | |
}; | |
MyAwaitedAction += async () => { | |
Log("Action 2 starting"); | |
await Task.Delay(5000); | |
Log("Action 2 finished"); | |
return false; | |
}; | |
MyAwaitedAction += () => { | |
Log("Action 3 starting"); | |
Log("Action 3 finished"); | |
return Task.FromResult(false); | |
}; | |
MyAwaitedAction += DoSomethingInteresting; | |
Log("Starting..."); | |
var results = await MyAwaitedAction.WhenAll(); | |
foreach (var result in results) | |
{ | |
MyAction(result); | |
} | |
Log("Done!"); | |
} | |
public void Log(string message) | |
{ | |
Log(DateTime.Now, message); | |
} | |
public void Log(DateTime when, string message) | |
{ | |
Console.WriteLine($"{when:s} {message}"); | |
} | |
private async Task<bool> DoSomethingInteresting() | |
{ | |
Log("Something interesting starting"); | |
await Task.Delay(2500); | |
Log("Something interesting finished"); | |
return true; | |
} | |
} | |
public static class FuncTaskTExtensions | |
{ | |
public static Task<T[]> WhenAll<T>(this Func<Task<T>> func) | |
{ | |
var tasks = func.GetInvocationList() | |
.OfType<Func<Task<T>>>() | |
.Select(d => d()); | |
return Task.WhenAll(tasks); | |
} | |
} | |
} | |
/* | |
2020-05-04T14:44:08 Setup | |
2020-05-04T14:44:08 Starting... | |
2020-05-04T14:44:08 Action 1 starting | |
2020-05-04T14:44:08 Action 2 starting | |
2020-05-04T14:44:08 Action 3 starting | |
2020-05-04T14:44:08 Action 3 finished | |
2020-05-04T14:44:08 Something interesting starting | |
2020-05-04T14:44:11 Something interesting finished | |
2020-05-04T14:44:11 Action 1 finished | |
2020-05-04T14:44:13 Action 2 finished | |
2020-05-04T14:44:13 True (0) | |
2020-05-04T14:44:13 True (1) | |
2020-05-04T14:44:13 True (2) | |
2020-05-04T14:44:13 False (0) | |
2020-05-04T14:44:13 False (1) | |
2020-05-04T14:44:13 False (2) | |
2020-05-04T14:44:13 False (0) | |
2020-05-04T14:44:13 False (1) | |
2020-05-04T14:44:13 False (2) | |
2020-05-04T14:44:13 True (0) | |
2020-05-04T14:44:13 True (1) | |
2020-05-04T14:44:13 True (2) | |
2020-05-04T14:44:13 Done!*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment