国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

ThreadStart和ParameterizedThreadStart區(qū)別

2019-11-17 02:34:24
字體:
供稿:網(wǎng)友

ThreadStart和ParameterizedThreadStart區(qū)別

ThreadStart:

ThreadStart這個委托定義為void ThreadStart(),也就是說,所執(zhí)行的方法不能有參數(shù)。

ThreadStart threadStart=new ThreadStart(Calculate);Thread thread=new Thread(threadStart);thread.Start();public void Calculate()   { double Diameter=0.5; Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);   }

這里我們用定義了一個ThreadStart類型的委托,這個委托制定了線程需要執(zhí)行的方法: Calculate,在這個方法里計算了一個直徑為0.5的圓的周長,并輸出.這就構(gòu)成了最簡單的多線程的例子,在很多情況下這就夠用了

ParameterThreadStart:

ParameterThreadStart的定義為void ParameterizedThreadStart(object state),使用這個這個委托定義的線程的啟動函數(shù)可以接受一個輸入?yún)?shù),具體例子如下 :

ParameterizedThreadStart threadStart=new ParameterizedThreadStart(Calculate)Thread thread=new Thread() ;thread.Start(0.9);public void Calculate(object arg){double Diameter=double(arg);Console.Write("The Area Of Circle with a Diameter of {0} is {1}"Diameter,Diameter*Math.PI);}

Calculate方法有一個為object類型的參數(shù),雖然只有一個參數(shù),而且還是object類型的,使用的時候尚需要類型轉(zhuǎn)換,但是好在可以有參數(shù)了,并且通過把多個參數(shù)組合到一個類中,然后把這個類的實例作為參數(shù)傳遞,就可以實現(xiàn)多個參數(shù)傳遞.比如:

class AddParams{    public int a, b;    public AddParams(int numb1, int numb2)    {      a = numb1;      b = numb2;    }}#endregionclass PRogram{    static void Main(string[] args)    {      Console.WriteLine("***** Adding with Thread objects *****");      Console.WriteLine("ID of thread in Main(): {0}",        Thread.CurrentThread.ManagedThreadId);      AddParams ap = new AddParams(10, 10);      Thread t = new Thread(new ParameterizedThreadStart(Add));      t.Start(ap);      Console.ReadLine();    }    #region Add method    static void Add(object data)    {      if (data is AddParams)      {        Console.WriteLine("ID of thread in Main(): {0}",          Thread.CurrentThread.ManagedThreadId);        AddParams ap = (AddParams)data;        Console.WriteLine("{0} + {1} is {2}",          ap.a, ap.b, ap.a + ap.b);      }    }    #endregion}}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 娄烦县| 建平县| 台山市| 思茅市| 德兴市| 凤山市| 永平县| 五大连池市| 项城市| 姚安县| 明星| 汾阳市| 富裕县| 班玛县| 皮山县| 石渠县| 宝丰县| 平山县| 揭西县| 洞头县| 西畴县| 隆德县| 武宁县| 青海省| 唐河县| 裕民县| 新民市| 阆中市| 梁河县| 茶陵县| 绥江县| 西充县| 金山区| 贡觉县| 景洪市| 若羌县| 珲春市| 卢龙县| 友谊县| 宝应县| 古田县|