using System ;
using System.Threading ;
namespace Test_Thread
{
public class MyThread
{
public void TestThread ( )
{
for (int i = 0 ; i < 3 ; i++)
{
Console.WriteLine ("執行緒 : {0} " , i ) ;
}
}
}
public class Test
{
public static void Main ( )
{
Console.WriteLine ("開始啟動執行緒:") ;
MyThread myThread1 = new MyThread ( ) ;
MyThread myThread2 = new MyThread ( ) ;
ThreadStart ts1 = new ThreadStart (myThread1.TestThread) ;
ThreadStart ts2 = new ThreadStart (myThread2.TestThread) ;
Thread a = new Thread ( ts1) ;
Thread b = new Thread ( ts2) ;
a.Start ( ) ;
b.Start ( ) ;
}
}
}
範例中首先使用"MyThread myThread1 = new MyThread ( ) ;"語法建立一個MyThread的實例
myThread1,然後使用"ThreadStart ts1 = new ThreadStart (myThread1.TestThread) ;"語法建立一個ThreadStart委派的實例 ts1,最後使用"Thread a = new Thread ( ts1) ;"語法將ts1傳給Thread類別的建構子,並建立了一個Thread類別的實例 a。
沒有留言:
張貼留言