当前位置:编程学习 > C#/ASP.NET >>

C# 多线程之新线程延时执行

  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApplication1
  5. {
  6. class Program
  7. {
  8. public static ManualResetEvent mre = new ManualResetEvent(false);
  9.  
  10. staticvoid Main(string[] args)
  11. {
  12. Thread newThread = new Thread(new ThreadStart(NewThread));
  13. newThread.Name = "新线程";
  14. newThread.Start();
  15.  
  16. mre.Set();
  17.  
  18. Console.WriteLine("Main 函数结束。");
  19. }
  20.  
  21. staticvoid NewThread()
  22. {
  23. Console.WriteLine("新线程: waiting for an event");
  24. mre.WaitOne();
  25. Console.WriteLine("新线程: got an event");
  26.  
  27. Console.WriteLine(DateTime.Now);
  28. Thread.Sleep(2000); //延时两秒
  29. Console.WriteLine(DateTime.Now);
  30.  
  31. Console.WriteLine("新线程已执行完。。");
  32. Console.WriteLine();
  33. }
  34. }
  35. }

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,