2010-03-22 17 views

उत्तर

41

मान लीजिए आप एक विधि है:

void A(string a, int b) {} 

यह (.NET 2.0) काम करना चाहिए:

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

thread.Start(); 

और निम्न (कम) उच्च संस्करणों के लिए:

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

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

thread.start() 
+0

'नई थ्रेड सूत्र में बाँधना (() => एक (" शब्द ", 10))' सी # 3 + के लिए। – Dykam

+0

बिल्कुल। मैं .NET 2.0 के बारे में सोच रहा था। –

+0

@Dykam, क्या आप "() =>" के अर्थ को आपके द्वारा दिए गए उदाहरण में विस्तृत कर सकते हैं (नया थ्रेड (() => ए ("शब्द", 10)) – Techee

4

समाधान tsocks सभी स्थितियों के लिए अच्छा नहीं हो सकता है आयनों क्योंकि यह निष्पादन के समय थ्रेडस्टार्ट प्रतिनिधि के निर्माण के समय पैरामीटर निर्दिष्ट करता है। यह त्रुटियों का कारण बन सकता है क्योंकि पैरामीटर निष्पादन से पहले बदल सकता है जो शायद आप नहीं चाहते हैं।

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

उत्पादन यहां इस प्रकार है::

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). 

सूचना है कि सभी प्रतिनिधियों के माध्यम से मूल्य 5, नहीं 0 मुद्रित मान लीजिए आप एक पाश में कई धागे बनाने की जरूरत है, यह प्रत्येक के साथ स्वयं के पैरामीटर है 4. ऐसा इसलिए है क्योंकि थ्रेडस्टार्ट प्रतिनिधि वैरिएबल "i" का उपयोग करते हैं, क्योंकि यह निष्पादन के समय है, प्रतिनिधि के निर्माण के समय नहीं। तो प्रतिनिधियों के निर्माण के पल के बाद पैरामीटर में कोई भी परिवर्तन (लूप में i ++) पैरामीटर के मान में प्रतिबिंबित होगा क्योंकि प्रतिनिधि निष्पादित करता है।

इस समस्या का समाधान पैरामीटरेटेड थ्रेडस्टार्ट और एक कस्टम क्लास का उपयोग करना है जो आपके सभी पैरामीटर को जोड़ता है (यदि अधिक हैं)। पैरामीटरयुक्त थ्रेडस्टार्ट के साथ, आप निष्पादन के समय पैरामीटर पास करते हैं। यह कुछ इस तरह दिखेगा:

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

उत्पादन यहाँ दिखाया गया है:

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). 

लिए

4

(निष्पादन का आदेश निर्धारित करने योग्य नहीं है, यह धागे के बीच एक दौड़ है) सी # 3.0 आप अज्ञात विधियों से गुजरने वाली बदसूरत ऑब्जेक्ट सरणी से बच सकते हैं:

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

सबसे आसान तरीका टी में से एक ओ पास पैरामीटर के रूप में

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) 
     { 

     } 
संबंधित मुद्दे