fork download
  1. using System;
  2. using System.Threading;
  3.  
  4. public class Test
  5. {
  6. private static void MyFunc(AutoResetEvent ev)
  7. {
  8. Thread.Sleep((int)(new Random().NextDouble() * 1000));
  9. ev.Set();
  10. }
  11.  
  12. public static void Main()
  13. {
  14. AutoResetEvent[] evs = {new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false)};
  15. Thread thread1 = new Thread(() => MyFunc(evs[0]));
  16. Thread thread2 = new Thread(() => MyFunc(evs[1]));
  17. Thread thread3 = new Thread(() => MyFunc(evs[2]));
  18.  
  19. thread1.Start();
  20. thread2.Start();
  21. thread3.Start();
  22.  
  23. int winner = WaitHandle.WaitAny(evs);
  24.  
  25. Console.WriteLine("The winner is thread{0}", winner + 1);
  26. }
  27. }
Success #stdin #stdout 0.04s 36928KB
stdin
Standard input is empty
stdout
The winner is thread1