在 Java 中,我们可以创建一个线程并传递一个 runnabe 对象,
new Thread( new Runnable() {
//Functions to happen
public void run() {
Textbox2.text="Selected";
}
}
).start();
.NET 中具有此功能的方式是什么?
在 Java 中,我们可以创建一个线程并传递一个 runnabe 对象,
new Thread( new Runnable() {
//Functions to happen
public void run() {
Textbox2.text="Selected";
}
}
).start();
.NET 中具有此功能的方式是什么?
练习这个例子
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);
}
}
}
}
大多数时候,您不必创建自己的线程。但是,如果您想要几乎相同的东西,请使用
new Thread(new ThreadStart(() => {
// do something
})).Start();