欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#給多線程傳參的幾種方式小結(jié)

 更新時(shí)間:2024年10月11日 10:53:25   作者:安替-AnTi  
本文詳細(xì)探討了如何在C#中進(jìn)行線程傳參,包括啟動(dòng)線程時(shí)如何將參數(shù)傳遞給線程函數(shù),以及在多線程環(huán)境下正確使用參數(shù)的方法,對(duì)于理解和實(shí)踐C#線程編程具有重要意義,需要的朋友可以參考下

前言

線程 被定義為程序的執(zhí)行路徑,每個(gè)線程執(zhí)行特定的工作。當(dāng)C#程序開(kāi)始時(shí),主線程自動(dòng)創(chuàng)建。

線程生命周期

  • 未啟動(dòng)狀態(tài)
  • 就緒狀態(tài)
  • 不可運(yùn)行狀態(tài)
  • 死亡狀態(tài)

創(chuàng)建無(wú)參Thread

void acceptThread(){
//TODO
}
Thread threadAccept = new Thread(new ThreadStart(acceptThread));
threadAccept.start();

創(chuàng)建有參Thread

給線程傳遞參數(shù)有兩種方式,一種方式是使用帶ParameterizedThreadStart委托參數(shù)的Thread構(gòu)造函數(shù),另一種方式是創(chuàng)建一個(gè)自定義類(lèi),把線程的方法定義為實(shí)例的方法,這樣就可以初始化實(shí)例的數(shù)據(jù),之后啟動(dòng)線程。

方式一:使用ParameterizedThreadStart委托

如果使用了ParameterizedThreadStart委托,線程的入口必須有一個(gè)object類(lèi)型的參數(shù),且返回類(lèi)型為void。且看下面的例子:

using System;
using System.Threading;

namespace ThreadWithParameters
{
    class Program
    {
        static void Main(string[] args)
        {
            string hello = "hello world";
            //這里也可簡(jiǎn)寫(xiě)成Thread thread = new Thread(ThreadMainWithParameters);
            //但是為了讓大家知道這里用的是ParameterizedThreadStart委托,就沒(méi)有簡(jiǎn)寫(xiě)了
            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);
        }
    }
}

這里稍微有點(diǎn)麻煩的就是ThreadMainWithParameters方法里的參數(shù)必須是object類(lèi)型的,我們需要進(jìn)行類(lèi)型轉(zhuǎn)換。為什么參數(shù)必須是object類(lèi)型呢,各位看看ParameterizedThreadStart委托的聲明就知道了。

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

方式二:創(chuàng)建自定義類(lèi)

定義一個(gè)類(lèi),在其中定義需要的字段,將線程的主方法定義為類(lèi)的一個(gè)實(shí)例方法。

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();
        }
    }
}

這種方法的缺點(diǎn)在于遇到一個(gè)耗時(shí)的方法,就新建一個(gè)類(lèi)。
那有什么更好辦法既不用強(qiáng)制類(lèi)型轉(zhuǎn)換,也不用新建一個(gè)類(lèi)呢?
使用匿名方法

方式三:使用匿名方法

using System;
using System.Threading;

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

            //如果寫(xiě)成Thread thread = new Thread(ThreadMainWithParameters(hello));這種形式,編譯時(shí)就會(huì)報(bào)錯(cuò)
            Thread thread = new Thread(() => ThreadMainWithParameters(hello));
            thread.Start();
            Console.Read();
        }

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

這樣既不用類(lèi)型強(qiáng)制轉(zhuǎn)換也不用新建類(lèi)就運(yùn)行成功了。

但是為什么這種方式能行呢,用ildasm反編譯后發(fā)現(xiàn),上述說(shuō)列出來(lái)的第三種方式其實(shí)和第二種方式是一樣的,只不過(guò)自定義類(lèi)編譯器幫我們做了。

下面的是第三種方式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

比較兩端代碼,可以發(fā)現(xiàn)兩者都有一個(gè)newobj,這句的作用是初始化一個(gè)類(lèi)的實(shí)例,第三種方式由編譯器生成了一個(gè)類(lèi):c__DisplayClass1

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

以上就是C#給多線程傳參的幾種方式小結(jié)的詳細(xì)內(nèi)容,更多關(guān)于C#多線程傳參方式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c#實(shí)現(xiàn)ini文件讀寫(xiě)類(lèi)分享

    c#實(shí)現(xiàn)ini文件讀寫(xiě)類(lèi)分享

    c#實(shí)現(xiàn)ini文件讀寫(xiě)類(lèi)分享,大家參考使用吧
    2013-12-12
  • C#中的事務(wù)用法實(shí)例分析

    C#中的事務(wù)用法實(shí)例分析

    這篇文章主要介紹了C#中的事務(wù)用法,以一個(gè)簡(jiǎn)單實(shí)例形式分析了C#創(chuàng)建及使用事物的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解

    C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解

    今天小編大家分享一篇C#獲得程序的根目錄以及判斷文件是否存在的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • C#?窗口過(guò)程消息處理?WndProc的方法詳解

    C#?窗口過(guò)程消息處理?WndProc的方法詳解

    在WinForm中一般采用重寫(xiě)WndProc的方法對(duì)窗口或控件接受到的指定消息進(jìn)行處理,本文給大家介紹C#窗口過(guò)程消息處理WndProc的方法詳解,感興趣的朋友一起看看吧
    2025-04-04
  • UnityShader實(shí)現(xiàn)百葉窗效果

    UnityShader實(shí)現(xiàn)百葉窗效果

    這篇文章主要為大家詳細(xì)介紹了UnityShader實(shí)現(xiàn)百葉窗效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C#中的WebRequest與WebResponse抽象類(lèi)、DNS靜態(tài)類(lèi)、Ping類(lèi)介紹

    C#中的WebRequest與WebResponse抽象類(lèi)、DNS靜態(tài)類(lèi)、Ping類(lèi)介紹

    這篇文章介紹了C#中的WebRequest與WebResponse抽象類(lèi)、DNS靜態(tài)類(lèi)、Ping類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • 最新評(píng)論