2010-03-22 22 views

Trả lời

41

Giả sử bạn có một phương pháp:

void A(string a, int b) {} 

này nên làm việc (.NET 2.0):

ThreadStart starter = delegate { A("word", 10); }; 
Thread thread = new Thread(starter); 

thread.Start(); 

Và sau (ngắn hơn) cho các phiên bản cao hơn:

ThreadStart starter =() => A("word", 10); 
Thread thread = new Thread(starter); 

//or just... 
//Thread thread = new Thread(() => A("word",10)); 

thread.start() 
+0

'new Thread (() => A ("chữ", 10)) 'cho C# 3 +. – Dykam

+0

Chính xác. Tôi đã nghĩ đến .NET 2.0. –

+0

@Dykam, bạn có thể xây dựng ý nghĩa của "() =>" trong ví dụ do bạn đưa ra (chủ đề mới (() => A ("từ", 10))) – Techee

4

Các giải pháp tsocks đã đưa ra có thể không tốt cho tất cả các địa điểm các ion vì nó chỉ định các tham số tại thời điểm tạo đại biểu ThreadStart, thay vì tại thời điểm thực hiện. Điều này có thể dẫn đến lỗi vì các tham số có thể thay đổi trước khi thực hiện có lẽ không phải là những gì bạn muốn. Giả sử bạn cần phải tạo ra một số đề trong một vòng lặp, mỗi với nó là các thông số riêng:

void CreateAndRunThreads() 
{ 
    List<ThreadStart> threadStartsList = new List<ThreadStart>(); 

    //delegate creation 
    for (int i = 0; i < 5; i++) 
    { 
     ThreadStart ts = delegate() { PrintInteger(i); }; 
     threadStartsList.Add(ts); 
    } 

    //delegate execution (at this moment i=5 in the previous loop) 
    foreach(ThreadStart ts in threadStartsList) 
    { 
     Thread t = new Thread(ts); 
     t.Start(); 
    } 
} 
private void PrintInteger(int i) 
{ 
    Debug.WriteLine("The integer value: "+i); 
} 

Sản lượng ở đây là như sau:

The integer value: 5 
The thread 0x17f0 has exited with code 0 (0x0). 
The integer value: 5 
The integer value: 5 
The thread 0x10f4 has exited with code 0 (0x0). 
The integer value: 5 
The thread 0x1488 has exited with code 0 (0x0). 
The integer value: 5 
The thread 0x684 has exited with code 0 (0x0). 

Chú ý rằng tất cả các đại biểu in giá trị 5, không phải 0 qua 4. Điều này là do các đại biểu ThreadStart sử dụng biến "i", vì nó là tại thời điểm thực hiện, không phải tại thời điểm tạo đại biểu. Vì vậy, bất kỳ thay đổi (i + + trong vòng lặp) để tham số sau thời điểm tạo ra các đại biểu sẽ được phản ánh trong giá trị của tham số như các đại biểu thực hiện.

Một giải pháp cho vấn đề này là sử dụng ParameterizedThreadStart và một lớp tùy chỉnh tổng hợp tất cả các tham số của bạn (nếu có nhiều hơn). Với parameterizedThreadStart, bạn chuyển các tham số tại thời điểm thực hiện. Điều này sẽ giống như thế này:

class CustomParameters 
{ 
    public int IntValue { get; set; } 
    public string FriendlyMessage { get; set; } 
} 

private void CreateAndRunThreadsWithParams() 
{ 
    List<ParameterizedThreadStart> threadStartsList = new List<ParameterizedThreadStart>(); 

    //delegate creation 
    for (int i = 0; i < 5; i++) 
    { 
     ParameterizedThreadStart ts = delegate(object o) { PrintCustomParams((CustomParameters)o); }; 
     threadStartsList.Add(ts); 
    } 

    //delegate execution 
    for (int i=0;i<threadStartsList.Count;i++) 
    { 
     Thread t = new Thread(threadStartsList[i]); 
     t.Start(new CustomParameters() { IntValue = i, FriendlyMessage = "Hello friend! Your integer value is:{0}"}); 
    } 
} 

private void PrintCustomParams(CustomParameters customParameters) 
{ 
    Debug.WriteLine(string.Format(customParameters.FriendlyMessage, customParameters.IntValue)); 
} 

Kết quả được hiển thị ở đây:

Hello friend! Your integer value is:1 
The thread 0x1510 has exited with code 0 (0x0). 
Hello friend! Your integer value is:0 
The thread 0x13f4 has exited with code 0 (0x0). 
Hello friend! Your integer value is:2 
The thread 0x157c has exited with code 0 (0x0). 
Hello friend! Your integer value is:3 
The thread 0x14e4 has exited with code 0 (0x0). 
Hello friend! Your integer value is:4 
The thread 0x1738 has exited with code 0 (0x0). 

(Trình tự thực hiện là không xác định, đó là một cuộc đua giữa các chủ đề)

4

Đối với C# 3.0 bạn có thể tránh mảng đối tượng xấu đi qua bằng các phương thức ẩn danh:

void Run() 
{ 
    string param1 = "hello"; 
    int param2 = 42; 

    Thread thread = new Thread(delegate() 
    { 
     MyMethod(param1,param2); 
    }); 
    thread.Start(); 
} 

void MyMethod(string p,int i) 
{ 

} 
0

một trong những cách đơn giản nhất t o tham số đường chuyền cho chủ đề như

Thread xmlThread =new Thread(()=>WriteLog(LogTypes.Message, "Flag", "Module", "Location", "Text", "Stacktrace")); 
       xmlThread.Start(); 



private object WriteLog(LogTypes logTypes, string p, string p_2, string p_3, string p_4, string p_5) 
     { 

     } 
0
public void Start() 
     { 
      var t1 = new Thread((message) => { Console.WriteLine(message); }); 
      //the parametirized threadstart accepts objects, it is not generic 
      var t2 = new Thread(number => { var num = (int)number; 
      Console.WriteLine(num++); 
      }); 
      var t3 = new Thread((vals)=> work(vals)); 

      t1.Start(); 
      t2.Start(); 
      t3.Start(20); 
     } 

     public void work(params object[] vals) 
     { 

     } 
Các vấn đề liên quan