2010-08-15 5 views
0

Salut c'est mes codes de fil multi-il fonctionne très bienutilisation de la fonction paramétrique en fil en 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; 
      } 
     } 
    } 
} 

mais je veux diviser les emplois en fonction getPerformance. donc je dois l'utiliser comme

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

etc dans ce cas visual studio me donne l'erreur « Nom de la méthode prévue ». quelle est la solution ?

edit: J'ai essayé, mais je ne pourrais pas adapté au format de tableau travail @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

plate-forme, la langue? Ca ressemble à C#, mais c'est un petit effort pour le mentionner. –

+0

désolé je l'ai oublié, et édité maintenant .. – Rapunzo

+0

getPerformance s'exécutera sur un thread non-ui, donc les changements à progressBar1 ne fonctionnera pas. En outre, plusieurs threads peuvent changer de propriétés simultanément, rendant votre code collé sujet aux problèmes de threading. – sisve

Répondre

3

1) Vous devriez probablement utiliser la Threadpool ou tâches (Ix4).

2) Lorsque vous pouvez utiliser délégués lamba'a (.NET 3 et versions ultérieures)

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

Pour les anciennes versions, vous pouvez utiliser ParameterizedThreadStart et un objet d'aide pour tenir début et de fin.


Modifier pour adapter votre boucle:

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

puis-je l'utiliser pour multithread? parce que cette variable "t" n'est pas un type de tableau – Rapunzo

+0

Oui, vous pouvez l'adapter à votre code. 't' est de type Thread. –

+0

@Rapunzo: Je ne peux pas lire cela, ajouter une section à votre question. –