-2

在 Java 中,我们可以创建一个线程并传递一个 runnabe 对象,

  new Thread( new Runnable() {
//Functions to happen
        public void run() {
 Textbox2.text="Selected";
}
}
).start(); 

.NET 中具有此功能的方式是什么?

4

2 回答 2

0

练习这个例子

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);
      }
    }
  }
}
于 2013-09-14T05:50:15.580 回答
0

大多数时候,您不必创建自己的线程。但是,如果您想要几乎相同的东西,请使用

new Thread(new ThreadStart(() => {
    // do something
})).Start();
于 2013-09-14T05:49:32.880 回答