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();
}
plate-forme, la langue? Ca ressemble à C#, mais c'est un petit effort pour le mentionner. –
désolé je l'ai oublié, et édité maintenant .. – Rapunzo
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