在下面的函數中,我應該如何替換「SomeType」? 代表似乎是錯在這裏...NET線程 - 快速問題
public static void StartThread(SomeType target)
{
ThreadStart tstart = new ThreadStart(target);
Thread thread = new Thread(tstart);
thread.Start();
}
編輯:我不是在尋找其它方法來寫這個。
在下面的函數中,我應該如何替換「SomeType」? 代表似乎是錯在這裏...NET線程 - 快速問題
public static void StartThread(SomeType target)
{
ThreadStart tstart = new ThreadStart(target);
Thread thread = new Thread(tstart);
thread.Start();
}
編輯:我不是在尋找其它方法來寫這個。
嘗試使用System.Action類型。
這裏我的測試代碼:
static void Main(string[] args)
{
StartThread(() => Console.WriteLine("Hello World!"));
Console.ReadKey();
}
public static void StartThread(Action target)
{
ThreadStart tstart = new ThreadStart(target);
Thread thread = new Thread(tstart);
thread.Start();
}
你的回答沒有錯,但是在這種情況下使用ThreadStart並不是更清楚,因爲這是新的Thread()所帶來的? – 2010-09-18 23:12:05
我明白你的意思。你是對的。實際上,如果我重寫方法,這是一個更好的方法。但我的問題的確切答案似乎是System.Action。 – 2010-09-18 23:20:09
您應該有ThreadStart
作爲參數,而不是嘗試在方法中初始化它。
我可以但我想知道是否可以這樣做。 ThreadStart的參數是這樣的:ThreadStart(void()target)..我怎樣才能實現這個? – 2010-09-18 23:05:48
我想不會有Sometype,因爲你正在調用一些將被線程化的函數。不是嗎? Like 線程t =新線程(新線程啓動(function_name_here)); t.start();
和 空隙function_name_here() { 等等等等 }
FYI沒有返回類型,但VOID。
更換SomeType
與System.Threading.ThreadStart
或System.Threading.ParameterizedThreadStart
。
你是什麼意思,你不是在尋找替代方法來寫這個?除非「SomeType」被定義爲'delegate void SomeType()',否則你將不得不這樣做。 – 2010-09-18 23:05:34
我的意思是我只是想知道SomeType應該是什麼。除此之外,一個線程可以以許多方式寫入。 – 2010-09-18 23:08:42
字面上,SomeType應該是我剛纔所說的,代表'void SomeType()'(或者只是System.Action,或者當然,ThreadStart)的委託。 – 2010-09-18 23:10:27