Created
March 1, 2016 21:53
-
-
Save stanroze/9275958326be993af252 to your computer and use it in GitHub Desktop.
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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var t = new Test(); | |
t.Daemon(); | |
Console.WriteLine("end main"); | |
string input = Console.ReadLine(); | |
while (input != "x") | |
{ | |
t.Signal(); | |
input = Console.ReadLine(); | |
} | |
} | |
} | |
class Test | |
{ | |
private TaskCompletionSource<string> signaler; | |
private Task<string> WaitOnSignalAsync() | |
{ | |
signaler = new TaskCompletionSource<string>(); | |
return signaler.Task; | |
} | |
public void Signal() | |
{ | |
signaler.SetResult("signaler fired"); | |
} | |
private Task<string> WaitOnTimeAsync() | |
{ | |
var tcs = new TaskCompletionSource<string>(); | |
Task.Run(async () => | |
{ | |
await Task.Delay(10000); | |
tcs.SetResult("timer fired"); | |
}); | |
return tcs.Task; | |
} | |
internal async Task Daemon() | |
{ | |
while (true) | |
{ | |
Console.WriteLine("waiting for input..."); | |
var result = await Task.WhenAny<string>(WaitOnTimeAsync(), WaitOnSignalAsync()).Unwrap(); | |
Console.WriteLine(result); | |
Console.WriteLine("doing something..."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment