2010-08-15 33 views
0

你好,這是我的多線程代碼,它工作正常使用參數化功能的線程在C#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.Threading; 

namespace searchIpAdresses 
{ 
    public partial class frmSearchIpRange : Form 
    { 
     public frmSearchIpRange() 
     { 
      InitializeComponent(); 
     } 

     int intStartIp, intEndIp; 

     private void frmSearchIpRange_Load(object sender, EventArgs e) 
     { 
      startIp.Text = "0.0.0.0"; 
      endIp.Text = "0.1.0.0"; 
      nudThreads.Value = 1; 
     } 

     private bool calcIpAddressRange() 
     { 
      intStartIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(startIp.Text).GetAddressBytes(), 0)); 
      intEndIp = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(IPAddress.Parse(endIp.Text).GetAddressBytes(), 0)); 
      if (intStartIp>intEndIp) 
      { 
       MessageBox.Show("End Ip must be bigger than Begin Ip!"); 
       return false; 
      } 
      else 
      { 
       return true; 
      } 
     } 

     private void btnShow_Click(object sender, EventArgs e) 
     { 
      if (calcIpAddressRange()) 
      { 
       int threadCount = Convert.ToInt32(nudThreads.Value); 
       Thread[] threads = new Thread[threadCount];    

       for (int i = 0; i < threadCount; i++) 
       { 
        threads[i] = new Thread(new ThreadStart(getPerformance));     
        threads[i].Name = string.Format(i.ToString()); 
       } 

       foreach (Thread t in threads) 
       { 
        t.Start(); 
       } 

       //for (int i = 0; i < nudThreads.Value; i++) 
       //{ 
       // if (threads[i].IsAlive) 
       //  threads[i].Abort(); 
       //} 
      } 

     } 

     private void getPerformance()//int sampleStartIp, int sampleEndIp 
     { 
      if (calcIpAddressRange()) 
      { 
       lbAddress.Items.Clear(); 
       progressBar1.Maximum = intEndIp - intStartIp; 
       int i; 
       for (i = intStartIp; i < intEndIp; i++) 
       { 
        string ipAddress = new IPAddress(BitConverter.GetBytes(IPAddress.NetworkToHostOrder(i))).ToString(); 

        progressBar1.Value++; 
        lbAddress.Items.Add(ipAddress); 
       } 
       MessageBox.Show(i.ToString() + " addresses added", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       progressBar1.Value = 0; 
      } 
     } 
    } 
} 

,但我想在getPerformance功能拆分工作。所以我在這種情況下,Visual Studio中使用它像

threads[i] = new Thread(new ThreadStart(getPerformance(sampleStart, sampleEnd)))

等給了我錯誤「預期的方法名」。解決辦法是什麼 ?

編輯:我試過,但我想不出它適應陣列格式工作@Henk Holterman

Thread[] threads = new Thread[threadCount];    

       for (int i = 0; i < threadCount; i++) 
       { 
        //threads[i] = new Thread(new ThreadStart(getPerformance));     
        threads[i].Name = string.Format(i.ToString()); 

        Thread t = new Thread(() => getPerformance(intStartIp, intEndIp)); 
       } 

       foreach (Thread t in threads) 
       { 
        t.Start(); 
       } 
+0

平臺,語言?它看起來像C#,但提到它是一個很小的努力。 – 2010-08-15 09:40:57

+0

抱歉,我忘了它,現在編輯.. – Rapunzo 2010-08-15 10:50:18

+0

getPerformance將在非ui線程上執行,因此對progressBar1的更改將不起作用。此外,多個線程可能會同時更改屬性,使得粘貼的代碼容易出現線程問題。 – sisve 2010-08-15 11:27:10

回答

3

1)你也許應該使用線程池或任務(FX4)。

2)當你可以使用 代表 lamba'a(.NET 3及更高版本)

int a=1, b=2; 
    Thread t = new Thread(() => Worker(a, b)); 

對於老版本,你可以使用ParameterizedThreadStart和輔助對象持有起點和終點。


編輯,以適應你的循環:

for (int i = 0; i < threadCount; i++) 
{ 
    // some code to update intStartIp, intEndIp 

    //threads[i] = new Thread(new ThreadStart(getPerformance));     
    threads[i] = new Thread(() => getPerformance(intStartIp, intEndIp)); 
    threads[i].Name = string.Format(i.ToString()); 
} 
+0

我可以使用這個多線程?因爲這個「t」變量不是一個數組類型 – Rapunzo 2010-08-15 10:43:38

+0

是的,你可以適應這個代碼。 't'是Thread的類型。 – 2010-08-15 10:59:58

+0

@Rununzo:我看不懂,爲你的問題添加一個部分。 – 2010-08-15 11:18:27