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

📄 sample51.cs

📁 C#函数手册
💻 CS
字号:
namespace apiBook
{
	using System;
	using System.Threading;
	public class TestMutexClass 
	{
		static int num = 8;
		static AutoResetEvent autoResetEvent= new AutoResetEvent(false);
		static TMutexClass t = new TMutexClass();
		public static void Main() 
		{
			for (int i= 0; i< num; i++) 
			{
				ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateInfo), i);
			}
			autoResetEvent.WaitOne();
			Console.WriteLine("操作结束。");
			Console.ReadLine();
		}
		static void UpdateInfo(Object obj) 
		{
			t.Access((int)obj);
			if (Interlocked.Decrement(ref num) == 0)
				autoResetEvent.Set();
		}
	}
	class TMutexClass
	{
		Mutex test = new Mutex();
		public void Access(int count) 
		{
			test.WaitOne();
			try 
			{
				Console.WriteLine("开始第"+count+"线程。");
				Thread.Sleep(200);
				Console.WriteLine("停止访问第"+count+"线程。");
			}
			finally 
			{
				test.ReleaseMutex();
				//使用ReleaseMutex方法释放Mutex对象
		
			}
		}
	}
}

⌨️ 快捷键说明

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