📄 ex-20-01
字号:
// Example 20-01: Using threads
namespace Programming_CSharp
{
using System;
using System.Threading;
class Tester
{
static void Main()
{
// make an instance of this class
Tester t = new Tester();
// run outside static Main
t.DoTest();
}
public void DoTest()
{
// create a thread for the Incrementer
// pass in a ThreadStart delegate
// with the address of Incrementer
Thread t1 =
new Thread(
new ThreadStart(Incrementer) );
// create a thread for the Decrementer
// pass in a ThreadStart delegate
// with the address of Decrementer
Thread t2 =
new Thread(
new ThreadStart(Decrementer) );
// start the threads
t1.Start();
t2.Start();
}
// demo function, counts up to 1K
public void Incrementer()
{
for (int i =0;i<1000;i++)
{
Console.WriteLine(
"Incrementer: {0}", i);
}
}
// demo function, counts down from 1k
public void Decrementer()
{
for (int i = 1000;i>=0;i--)
{
Console.WriteLine(
"Decrementer: {0}", i);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -