2010-05-11 133 views
5

我想通過一個函數,它的一個參數的ThreadStart構造函數,C#。但是,看來這是不可能的,因爲我得到一個語法錯誤是我嘗試做這樣的事情傳遞參數給線程

Thread t1 = new Thread(new ThreadStart(func1(obj1)); 

其中OBJ1是List<string>類型的對象(說)。

如果我想要一個線程來執行此功能,在一個對象作爲參數,我打算同時創建2級這樣的線程具有不同的參數值是什麼,是實現這一目標的最佳方法是什麼?

+1

您正在使用什麼版本的.NET的? – 2010-05-11 16:57:34

+0

哇!我之前通過創建一個包含該線程將使用的數據的類來實現此目的,但我不知道有這麼多種不同的方法來對此動物進行蒙皮! – 2010-05-11 17:01:45

回答

7

你需要ParametrizedThreadStart一個參數傳遞給線程。

Thread t1 = new Thread(new ParametrizedThreadStart(func1); 
t1.Start(obj1); 
+0

非常感謝..這種方法工程..然而,func1在這種情況下的定義必須是 void func1(對象狀態) 並且不能是 void func1(List obj1) 爲什麼會出現這種情況? – assassin 2010-05-12 05:05:33

+0

這就是'ParametrizedThreadStart'委託類型的定義方式。 ''''''''''''''''''''''''''''''''''''''''''''必須在'func1'裏面把'object'放回'List ' – Thomas 2010-05-12 06:34:54

13

如果您使用.NET 3.5或更高版本,一個選擇是使用這個拉姆達:

var myThread = new System.Threading.Thread(() => func1(obj1)); 
+0

非常感謝! – assassin 2010-05-12 05:07:38

3

試試這個:

var bar = 0.0; 
Thread t = new Thread(() => 
    { 
     Foo(bar); 
    }); 
t.IsBackground = true; 
t.Start(); 

或者你的情況:

Object obj1 = new Object(); 
Thread t = new Thread(() => 
    { 
     func1(obj1); 
    }); 
t.IsBackground = true; 
t.Start(); 
7

你可以像這樣開始一個新的線程:

Thread thread = new Thread(delegate() { 
    // Code here. 
}); 
thread.Start(); 

裏面的anonymous method您有權訪問是在範圍上創建委託時的變量。

3

編輯刺客有麻煩此代碼工作,所以我已經包括在這篇文章的最後一個完整的示例控制檯應用程序。

 


{ // some code 
    Thread t1 = new Thread(MyThreadStart); 
    t1.Start(theList); 
} 

void MyThreadStart(object state) 
{ 
    List<string> theList = (List<string>)state; 
    //.. 
} 

 

這是我的編輯:下面是一個完整的控制檯應用程序 - 該技術確實工作:

 

using System; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      System.Threading.Thread t = new System.Threading.Thread(MyThreadStart); 
      t.Start("Hello"); 
      System.Console.ReadLine(); 
     } 


     static void MyThreadStart(object state) 
     { 
      System.Console.WriteLine((string)state); 
     } 
    } 
} 

 
+0

這不起作用..我試過了...托馬斯建議的ParametrizedThreadStart方法。 – assassin 2010-05-12 05:03:59

+0

@assassin我向你保證,它確實有效。我編輯了我的條目以包含一個控制檯應用程序,您可以直接將其粘貼到Visual Studio中並運行。此語法自.Net 2.0開始工作,如果將「新線程(MyThreadStart)」替換爲「新線程(新代理 – JMarsch 2010-05-12 14:05:40

3

這是你要找的影響嗎?

 static void Main(string[] args) 
    { 
     var list = new List<string>(){ 
      "a","b","c" 
     }; 

     Thread t1 = new Thread(new ParameterizedThreadStart(DoWork)); 

     t1.Start(list); 

     Console.ReadLine(); 

    } 

    public static void DoWork(object stuff) 
    { 
     foreach (var item in stuff as List<string>) 
     { 
      Console.WriteLine(item); 
     } 
    } 
1
static void func1(object parameter) 
{ 
    // Do stuff here. 
} 

static void Main(string[] args) 
{ 
    List<string> obj1 = new List<string>(); 
    Thread t1 = new Thread(func1); 
    t1.Start(obj1); 
} 

它使用.NET 2.0中新的代表呼籲ParameterizedThreadStart。你可以閱讀關於它here

0

你絕對需要使用Thread對象?或者你只是在尋找多線程處理髮生?更「現代」的方法是使用異步委託作爲這樣:

private delegate void FuncDelegate(object obj1); 
. 
. 
. 
FuncDelegate func = func1; 
IAsyncResult result = func.BeginInvoke(obj1, Completed, func); 

// do other stuff 
. 
. 
. 

private void Completed(IAsyncResult result) 
{ 
    ((FuncDelegate)result.AsyncState).EndInvoke(result); 

    // do other cleanup 
} 

甚至更​​「現代」的方法是在.NET 4 TPL使用Tasks