class1.cs

来自「原代码详细说明是关于c++方面的希望可以帮助大家使用」· CS 代码 · 共 48 行

CS
48
字号
using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
	class MyMainClass
	{
		//Initially not signaled.
		const int numIterations = 100;
		static AutoResetEvent myResetEvent = new AutoResetEvent(false);
		static int number;
      
		static void Main()
		{
			//Create and start the reader thread.
			Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
			myReaderThread.Name = "ReaderThread";
			myReaderThread.Start();

			for(int i = 1; i <= numIterations; i++)
			{
				Console.WriteLine("Writer thread writing value: {0}", i);
				number = i;
            
				//Signal that a value has been written.
				myResetEvent.Set();
            
				//Give the Reader thread an opportunity to act.
				Thread.Sleep(0);
			}

			//Terminate the reader thread.
			myReaderThread.Abort();
		}

		static void MyReadThreadProc()
		{
			while(true)
			{
				//The value will not be read until the writer has written
				// at least once since the last read.
				myResetEvent.WaitOne();
				Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number);
			}
		}
	}
}

⌨️ 快捷键说明

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