Created
December 17, 2021 15:29
-
-
Save Rowandish/99bff26bbbb496c3d714b6950cf00d9b to your computer and use it in GitHub Desktop.
Task synchronization with ManualResetEvent
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) | |
{ | |
using var signal = new ManualResetEvent(false); | |
var t1 = Task.Run(() => | |
{ | |
Runner(signal, 1); | |
}); | |
var t2 = Task.Run(() => | |
{ | |
Runner(signal, 2); | |
}); | |
Task.WhenAll(t1, t2).ContinueWith(task => signal.Reset()); | |
Thread.Sleep(1000); // operation | |
signal.Set(); | |
Console.ReadLine(); | |
} | |
static void Runner(ManualResetEvent signal, int id) | |
{ | |
Console.WriteLine($"Runner [{id}] is ready"); | |
signal.WaitOne(); | |
Console.WriteLine($"Runner [{id}] start!"); | |
Thread.Sleep(1000); // operation | |
Console.WriteLine($"Runner [{id}] finished!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment