C#用委託傳遞Thread參數及其泛型封裝

來源:互聯網
上載者:User


使用ParameterizedThreadStart委託建立的Thread可以調用Start(Object)傳參,當方法不具有與ParameterizedThreadStart相容的簽名和傳回型別時,需要對方法進行封裝,其中就包括如何處理傳回值和out/ref參數的問題。

比如線上程中要執行下面的方法

public static string testFunc (ref double a, int b, out double c){double d = a + 0.01;c = d;string s = "a=" + a + ";b=" + b + ";c=" + d;a = a + b;return s;}

可以看出.NET內建的委託(Action,Func,Predicate)都不適用於testFunc,下面自己聲明一個委託

public delegate string TestDelegate (ref double a, int b, out double c);

在執行個體化Thread的時候不直接把testFunc傳過去,在傳過去的匿名函數中被調用的是一個以testFunc作為參數的Action<TestDelegate>執行個體,而這個執行個體作為參數在Thread.Start中傳入,對testFunc的調用及其參數和傳回值的處理寫在這個Action<TestDelegate>執行個體之中。

static void Main (string[] args){Console.WriteLine ("Hello World!");testThread ();Thread.Sleep (1000);Console.WriteLine (s);}public static void testThread (){double a = 3.5, c;int b = 1;string s = string.Empty;Thread thread = new Thread ((object parameter) => {Action<TestDelegate> p = parameter as Action<TestDelegate>;if (p != null) {p (testFunc);}});thread.Start ((TestDelegate start) => s = start (ref a, b, out c));}

然後按上面的方法封裝成一個泛型Thread如下

class Thread<TStart> /* where TStart : Delegate */ // 約束不能是特殊類“System.Delegate”{Thread originalThread;public Thread OriginalThread { get { return originalThread; } }public Thread (TStart start){originalThread = new Thread (convertStart (start));}public Thread (TStart start, int maxStackSize){originalThread = new Thread (convertStart (start), maxStackSize);}ParameterizedThreadStart convertStart (TStart start){return (object parameter) => {Action<TStart> p = parameter as Action<TStart>;if (p != null) {p (start);}};}public void Start (Action<TStart> parameter){originalThread.Start (parameter);}}

在Main中加入調用代碼

static void Main (string[] args){Console.WriteLine ("Hello World!");testThread ();Thread.Sleep (1000);Console.WriteLine (s);double a = 3.5, c;int b = 1;string s = string.Empty;Thread<TestDelegate> thread = new Thread<TestDelegate> (testFunc);thread.Start ((TestDelegate start) => {for (int i = 1; i < 10; i++) {s = start (ref a, 2, out c);Console.WriteLine (s);Thread.Sleep (1000);}});for (int i = 1; i < 10; i++) {Console.WriteLine ("Hello World!");Thread.Sleep (1000);}}

下面被注釋的語句報編譯器錯誤CS1628“不能在匿名方法、lambda 運算式或查詢運算式內使用 ref 或 out 參數”

public static string testThread (ref double a, int b, out double c){string s = string.Empty;Thread<TestDelegate> thread = new Thread<TestDelegate> (testFunc);// thread.Start ((TestDelegate start) => s = start (ref a, b, out c));double a1 = a, c1 = default(double);thread.Start ((TestDelegate start) => s = start (ref a1, b, out c1));c = c1;return s;}

而值參數和局部變數方式則沒有這個限制,這涉及到C#的閉包。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.