fork download
  1. using System;
  2. using System.Threading;
  3.  
  4. class Program {
  5. public static void DoWork(object obj) {
  6. Console.WriteLine("Child thread starts");
  7.  
  8. if (obj is String)
  9. Console.WriteLine(obj as String);
  10. else
  11. throw new ArgumentException("Parameter is not a string.", nameof(obj));
  12.  
  13. Console.WriteLine("Child thread goes to sleep");
  14. Thread.Sleep(5000); // the thread is paused for 5000 milliseconds
  15. Console.WriteLine("Child thread resumes and finishes");
  16. }
  17.  
  18. static void Main(string[] args) {
  19. ParameterizedThreadStart childJob = new ParameterizedThreadStart(DoWork);
  20. Console.WriteLine("Main thread starts");
  21.  
  22. Thread childThread = new Thread(childJob);
  23. childThread.Start("Message from Main");
  24.  
  25. Console.WriteLine("Main thread waiting");
  26. childThread.Join();
  27. Console.WriteLine("Main thread finishes");
  28. }
  29. }
Success #stdin #stdout 0s 199104KB
stdin
Standard input is empty
stdout
Main thread starts
Child thread starts
Message from Main
Child thread goes to sleep
Main thread waiting
Child thread resumes and finishes
Main thread finishes