📄 class1.cs
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -