簡體   English   中英

線程和有什么不一樣

[英]Thread What is the difference between

之間有什么區別

Thread t = new Thread (new ThreadStart (Go));

Thread t = new Thread (Go);

Go是一種方法

唯一的區別是,如果Go是可以匹配多個Thread構造函數重載的方法組-例如,由於同時存在ThreadStartParameterizedThreadStart的構造函數,因此以下方法會使new Thread(Go)版本含糊不清:

static void Go() { }
static void Go(object val) { }

new Thread (new ThreadStart (Go))通過顯式聲明委托類型來消除歧義,但是:在C#2或更高版本上,除了它們是相同的以外,它們是相同的。 注意:在C#2之前,較短的版本不是合法語法。

沒有。 他們是一樣的東西。

文檔指出:

創建線程時,Visual Basic和C#用戶可以省略ThreadStartParameterizedThreadStart委托構造函數。 [...]在C#中,只需指定線程過程的名稱。 編譯器選擇正確的委托構造函數。

第二個是一個捷徑! 基本上,做同樣的事情。 但是,在對象threadstart內部有一組可以通知的參數。

沒有,我很煩。 Thread的構造函數采用ThreadStart “ construct”類型的delegate

// constructor of Thread
public Thread(ThreadStart start);

ThreadStart定義為:

namespace System.Threading
{
    // Summary:
    //     Represents the method that executes on a System.Threading.Thread.
    [ComVisible(true)]
    public delegate void ThreadStart();
}

由於每種方法都可以用作委托,因此您可以將您的方法直接傳遞給構造函數。 通過明確地寫..

new Thread(new ThreadStart(Go))

..您只需再次包裝即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM