⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample55.cs

📁 C#函数手册
💻 CS
字号:
namespace apiBook
{
	using System;
	using System.Threading;
	using System.Windows.Forms;
	public class ErrorHandler  
	{
		static int count=5;
		static AutoResetEvent autoResetEvent = new AutoResetEvent(false);
		public static void Main() 
		{
			for (int i= 0; i < 5; i++) 
			{
				ThreadPool.QueueUserWorkItem(new WaitCallback(DoPrint), i);
				//使用QueueUserWorkItem方法
			}		
			autoResetEvent.WaitOne();
			Console.WriteLine("所有线程打印完毕。");
			Console.WriteLine("Timeout.Infinite="+Timeout.Infinite);
			Thread t=new Thread(new ThreadStart(DoPrint));
			t.Start();			
			Console.ReadLine();
		}
		public static void DoPrint(Object obj)
		{
			int num=(int)obj;
			Console.WriteLine("当前执行第"+num+"线程。");
			if (Interlocked.Decrement(ref count) == 0)
				autoResetEvent.Set();
		}	
		public static void DoPrint()
		{
			Console.WriteLine("开始独立线程,请注意,等多久都没看到‘睡醒了’");
			Thread.Sleep(Timeout.Infinite);
			//使用Infinite字段初始化Sleep方法
			Console.WriteLine("睡醒了");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -