C#往線程里傳遞參數(shù)的方法小結(jié)
傳參方式有兩種:
1、創(chuàng)建帶參構(gòu)造方法類 傳參
2、利用Thread.start(8)直接傳參,該方法會(huì)接收一個(gè)對(duì)象,并將該對(duì)象傳遞給線程,因此在線程中啟動(dòng)的方法
必須接收object類型的單個(gè)參數(shù)。
Thread (ParameterizedThreadStart) 初始化 Thread 類的新實(shí)例,指定允許對(duì)象在線程啟動(dòng)時(shí)傳遞給線程的委托。
Thread (ThreadStart) 初始化 Thread 類的新實(shí)例。
由 .NET Compact Framework 支持。
Thread (ParameterizedThreadStart, Int32) 初始化 Thread 類的新實(shí)例,指定允許對(duì)象在線程啟動(dòng)時(shí)傳遞給線程的委托,并指定線程的最大堆棧大小。
Thread (ThreadStart, Int32) 初始化 Thread 類的新實(shí)例,指定線程的最大堆棧大小。
由 .NET Compact Framework 支持。
我們?nèi)绻x不帶參數(shù)的線程,可以用ThreadStart,帶一個(gè)參數(shù)的用ParameterizedThreadStart。帶多個(gè)參數(shù)的用另外的方法,下面逐一講述。
一、不帶參數(shù)的
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AAAAAA { class AAA { public static void Main() { Thread t = new Thread(new ThreadStart(A)); t.Start(); Console.Read(); } private static void A() { Console.WriteLine("Method A!"); } } }
結(jié)果顯示Method A!
二、帶一個(gè)參數(shù)的
由于ParameterizedThreadStart要求參數(shù)類型必須為object,所以定義的方法B形參類型必須為object。
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AAAAAA { class AAA { public static void Main() { Thread t = new Thread(new ParameterizedThreadStart(B)); t.Start("B"); Console.Read(); } private static void B(object obj) { Console.WriteLine("Method {0}!",obj.ToString ()); } } }
結(jié)果顯示Method B!
三、帶多個(gè)參數(shù)的
由于Thread默認(rèn)只提供了這兩種構(gòu)造函數(shù),如果需要傳遞多個(gè)參數(shù),我們可以自己將參數(shù)作為類的屬性。定義類的對(duì)象時(shí)候?qū)嵗@個(gè)屬性,然后進(jìn)行操作。
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AAAAAA { class AAA { public static void Main() { My m = new My(); m.x = 2; m.y = 3; Thread t = new Thread(new ThreadStart(m.C)); t.Start(); Console.Read(); } } class My { public int x, y; public void C() { Console.WriteLine("x={0},y={1}", this.x, this.y); } } }
結(jié)果顯示x=2,y=3
四、利用結(jié)構(gòu)體給參數(shù)傳值。
定義公用的public struct,里面可以定義自己需要的參數(shù),然后在需要添加線程的時(shí)候,可以定義結(jié)構(gòu)體的實(shí)例。
//結(jié)構(gòu)體 struct RowCol { public int row; public int col; }; //定義方法 public void Output(Object rc) { RowCol rowCol = (RowCol)rc; for (int i = 0; i < rowCol.row; i++) { for (int j = 0; j < rowCol.col; j++) Console.Write("{0} ", _char); Console.Write("\n"); } }
以上所述是小編給大家介紹的C#往線程里傳遞參數(shù)的方法小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于Unity C# Mathf.Abs()取絕對(duì)值性能測(cè)試詳解
這篇文章主要給大家介紹了關(guān)于Unity C# Mathf.Abs()取絕對(duì)值性能測(cè)試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Unity C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄
這篇文章主要介紹了C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01C#中的char、string和StringBuilder的使用詳解
這篇文章主要介紹了C#中的char、string和StringBuilder的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))
在工作中經(jīng)常遇到C#數(shù)組、ArrayList、List、Dictionary存取數(shù)據(jù),但是該選擇哪種類型進(jìn)行存儲(chǔ)數(shù)據(jù)呢?很迷茫,今天小編抽空給大家整理下這方面的內(nèi)容,需要的朋友參考下吧2017-02-02c#的時(shí)間日期操作示例分享(c#獲取當(dāng)前日期)
這篇文章主要介紹了c#的時(shí)間日期操作示例,在給定時(shí)間戳返回指定的時(shí)間格式和獲取當(dāng)前時(shí)間方法,需要的朋友可以參考下2014-03-03