C# 給多線程傳參的三種方式

來源:互聯網
上載者:User

從《C#進階編程》瞭解到給線程傳遞參數有兩種方式,一種方式是使用帶ParameterizedThreadStart委託參數的Thread建構函式,另一種方式是建立一個自訂類,把線程的方法定義為執行個體的方法,這樣就可以初始化執行個體的資料,之後啟動線程。

方式一:使用ParameterizedThreadStart委託

如果使用了ParameterizedThreadStart委託,線程的入口必須有一個object類型的參數,且傳回型別為void。且看下面的例子:

using System;
using System.Threading;

namespace ThreadWithParameters
{
class Program
{
static void Main(string[] args)
{
string hello = "hello world";

//這裡也可簡寫成Thread thread = new Thread(ThreadMainWithParameters);
//但是為了讓大家知道這裡用的是ParameterizedThreadStart委託,就沒有簡寫了
Thread thread = new Thread(new ParameterizedThreadStart(ThreadMainWithParameters));
thread.Start(hello);

Console.Read();
}

static void ThreadMainWithParameters(object obj)
{
string str = obj as string;
if(!string.IsNullOrEmpty(str))
Console.WriteLine("Running in a thread,received: {0}", str);
}
}
}

這裡稍微有點麻煩的就是ThreadMainWithParameters方法裡的參數必須是object類型的,我們需要進行類型轉換。為什麼參數必須是object類型呢,各位看看ParameterizedThreadStart委託的聲明就知道了。

public delegate void ParameterizedThreadStart(object obj);   //ParameterizedThreadStart委託的聲明

方式二:建立自訂類

    定義一個類,在其中定義需要的欄位,將線程的主方法定義為類的一個執行個體方法,說得不是很明白,還是看實際的例子吧。

using System;
using System.Threading;

namespace ThreadWithParameters
{
public class MyThread
{
private string data;

public MyThread(string data)
{
this.data = data;
}

public void ThreadMain()
{
Console.WriteLine("Running in a thread,data: {0}", data);
}
}

class Program
{
static void Main(string[] args)
{
MyThread myThread = new MyThread("hello world");

Thread thread = new Thread(myThread.ThreadMain);
thread.Start();

Console.Read();
}
}
}

對這種方法也不是很滿意,總不能一遇到比較耗時的方法,就建立一個類吧。。。

 

那有什麼更好辦法即不用強制類型轉換,也不用建立一個類呢?

下面就介紹下我無意中找到的一個方法,具體是在哪見過的我也不記得了,罪過啊。。

方式三:利用lambda運算式

 

using System;
using System.Threading;

namespace ThreadWithParameters
{
class Program
{
static void Main(string[] args)
{
string hello = "hello world";

//如果寫成Thread thread = new Thread(ThreadMainWithParameters(hello));這種形式,編譯時間就會報錯
Thread thread = new Thread(() => ThreadMainWithParameters(hello));
thread.Start();

Console.Read();
}

static void ThreadMainWithParameters(string str)
{
Console.WriteLine("Running in a thread,received: {0}", str);
}
}
}

哇,你會發現既不用類型強制轉換也不用建立類就運行成功了。

但是為什麼這種方式能行呢,根據昨天 @亂舞春秋 的提示,我也用ildasm反編譯了一下,確實如他所說,我所謂的第三種方式其實和第二種方式是一樣的,只不過自訂類編譯器幫我們做了。

下面的是第三種方式main方法反編譯的IL代碼:

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// 代碼大小 51 (0x33)
.maxstack 3
.locals init ([0] class [mscorlib]System.Threading.Thread thread,
[1] class ThreadWithParameters.Program/'<>c__DisplayClass1' 'CS$<>8__locals2')
IL_0000: newobj instance void ThreadWithParameters.Program/'<>c__DisplayClass1'::.ctor()
IL_0005: stloc.1
IL_0006: nop
IL_0007: ldloc.1
IL_0008: ldstr "hello world"

IL_000d: stfld string ThreadWithParameters.Program/'<>c__DisplayClass1'::hello
IL_0012: ldloc.1
IL_0013: ldftn instance void ThreadWithParameters.Program/'<>c__DisplayClass1'::'<Main>b__0'()
IL_0019: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_001e: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)
IL_0023: stloc.0
IL_0024: ldloc.0

IL_0025: callvirt instance void [mscorlib]System.Threading.Thread::Start()
IL_002a: nop
IL_002b: call int32 [mscorlib]System.Console::Read()
IL_0030: pop
IL_0031: nop
IL_0032: ret
} // end of method Program::Main

在看看第二種方式的IL代碼:

 .method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// 代碼大小 44 (0x2c)
.maxstack 3
.locals init ([0] class ThreadWithParameters.MyThread myThread,
[1] class [mscorlib]System.Threading.Thread thread)
IL_0000: nop
IL_0001: ldstr "hello world"
IL_0006: newobj instance void ThreadWithParameters.MyThread::.ctor(string)
IL_000b: stloc.0
IL_000c: ldloc.0

IL_000d: ldftn instance void ThreadWithParameters.MyThread::ThreadMain()
IL_0013: newobj instance void [mscorlib]System.Threading.ThreadStart::.ctor(object,
native int)
IL_0018: newobj instance void [mscorlib]System.Threading.Thread::.ctor(class [mscorlib]System.Threading.ThreadStart)
IL_001d: stloc.1
IL_001e: ldloc.1

IL_001f: callvirt instance void [mscorlib]System.Threading.Thread::Start()
IL_0024: nop
IL_0025: call int32 [mscorlib]System.Console::Read()
IL_002a: pop
IL_002b: ret
} // end of method Program::Main

比較兩端代碼,可以發現兩者都有一個newobj,這句的作用是初始化一個類的執行個體,第三種方式由編譯器產生了一個類:c__DisplayClass1

IL_0000:  newobj     instance void ThreadWithParameters.Program/'<>c__DisplayClass1'::.ctor()
IL_0006:  newobj     instance void ThreadWithParameters.MyThread::.ctor(string)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.