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

📄 operations.cs

📁 北大青鸟内部资料
💻 CS
字号:
using System;
using System.Threading;

namespace Example_5
{
	/// <summary>
	/// Summary description for Operations
	/// </summary>
	public class Operations
	{
		public int VarIncrement; 
		public int VarSquare;
		public int VarLoopValue;
		
		public System.Threading.Thread objSquare; 
		public System.Threading.Thread objIncrement; 
		public System.Threading.Thread objLoop;
		
		public delegate void SquareCompleteHandler(double Square);
		public delegate void IncrementCompleteHandler(double Result);
		public delegate void LoopCompleteHandler(int Counter);

		public event SquareCompleteHandler SquareComplete;
		public event IncrementCompleteHandler IncrementComplete;
		public event LoopCompleteHandler LoopComplete;

		public Operations()
		{
	    }

		// This method will calculate the square of the given no.
		public void Square()
		{
			double VarResult ;
			Console.WriteLine ("Running the square thread");
			VarResult = VarSquare * VarSquare;
			SquareComplete(VarResult);
		}

		// This method will add 9 to the given number.

		public void Increment()
		{
			int VarResult = VarIncrement + 9;
			Console.WriteLine ("Running the increment thread");
			IncrementComplete(VarResult);
		}

		// This method will run a loop with a nested loop VarLoopValue times.
		public void RunALoop()
		{
			int VarX;
			Console.WriteLine ("loop");
			for (VarX = 1; VarX <= VarLoopValue; VarX++)
			{
				Console.WriteLine ("Running the loop");
				//give a delay for some milliseconds
				Thread.Sleep(500); 
			}
			LoopComplete(VarLoopValue);
		}

		public void ChooseThreads(int threadNumber)
		{
			// Determines which thread to start based on the value it receives.
			switch(threadNumber)
			{
				case 1:
					// Sets the thread using the AddressOf the subroutine where
					// the thread will start.
					objSquare = new System.Threading.Thread(new
						System.Threading.ThreadStart(this.Square));
					// Starts the thread.
					objSquare.Start();
					break;
				case 2:
					objIncrement = new System.Threading.Thread(new
						System.Threading.ThreadStart(this.Increment));
					objIncrement.Start();
					break;
				case 3:
					objLoop = new System.Threading.Thread(new
						System.Threading.ThreadStart(this.RunALoop));
					objLoop.Start();
					break;
			}
		}
    }

}

⌨️ 快捷键说明

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