class1.cs

来自「《c#技术内幕代码》」· CS 代码 · 共 46 行

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

namespace Ch12ThreadConsole
{
	class Class1
	{
		Boolean bDone = false;
		protected void ThreadFunc()
		{
			int counter = 0;
			while ( !bDone )
			{
				Thread.Sleep(1000);
				counter ++;
				Console.WriteLine("Counter: {0}", counter);
			}
			Console.WriteLine("Terminating");
		}

		static void Main(string[] args)
		{
			Class1 c1 = new Class1();
			Thread fThread1 = new Thread( new ThreadStart(c1.ThreadFunc) );
			fThread1.Start();
			Thread fThread2 = new Thread( new ThreadStart(c1.ThreadFunc) );
			fThread2.Start();
			Thread fThread3 = new Thread( new ThreadStart(c1.ThreadFunc) );
			fThread3.Start();
			while ( c1.bDone == false )
			{
				Console.Write("Enter a string and press return to stop the app: ");
				string s = Console.ReadLine();
				if ( s == "1" )
					fThread1.Abort();
				if ( s == "2" )
					fThread2.Interrupt();
				if ( s == "3" )
					fThread3.Abort();
				if ( s == "a" || s == "A" )
					c1.bDone = true;
			}
		}
	}
}

⌨️ 快捷键说明

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