Task:用户委托方式

  1. public class Example
  2. {
  3. public static void Main()
  4. {
  5. Thread.CurrentThread.Name = "Main";
  6. // Create a task and supply a user delegate by using a lambda expression.
  7. Task taskA = new Task( () => Console.WriteLine("Hello from taskA."));
  8. // Start the task.
  9. taskA.Start();
  10. // Output a message from the calling thread.
  11. Console.WriteLine("Hello from thread '{0}'.",
  12. Thread.CurrentThread.Name);
  13. taskA.Wait();
  14. }
  15. }

Task.Run

  1. public class Example
  2. {
  3. public static void Main()
  4. {
  5. Thread.CurrentThread.Name = "Main";
  6. // Define and run the task.
  7. Task taskA = Task.Run( () => Console.WriteLine("Hello from taskA."));
  8. // Output a message from the calling thread.
  9. Console.WriteLine("Hello from thread '{0}'.",
  10. Thread.CurrentThread.Name);
  11. taskA.Wait();
  12. }
  13. }

TaskFactory.StartNew