SE la metto in
codice:
public Form1()
{
InitializeComponent();
progressBar1.Invoke(new BarDelegate(UpdateBar));
}
mi viene dato subito l'errore:
Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
Ti posto il codice così capisci meglio. La funzione pathping funziona così:
Viene chiamata dal pulsante:
codice:
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Step = 1;
progressBar1.Value = 0;
progressBar1.Minimum = 0;
progressBar1.Maximum = 1000;
.......
else if (checkBox3.Checked)
{
thread = new Thread(delegate() {pathping();});
thread.Start();
}
}
si avvia quindi pathping che è creata per fare lo stesso di pathping.exe
codice:
private void pathping()
{
IPAddress ipAddress = Dns.GetHostEntry(textBox1.Text).AddressList[0];
using (Ping pingSender = new Ping())
{
//setto parametri per connessione
PingOptions pingOptions = new PingOptions();
PingOptions pingOptions1 = new PingOptions();
Stopwatch stopWatch = new Stopwatch();
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
//byte[] bytes = new byte[32];
pingOptions.DontFragment = true;
pingOptions.Ttl = 1;
pingOptions1.DontFragment = true;
pingOptions1.Ttl = 30;
int maxHops = 30;
//scrivo su textbox
textBox2.BeginInvoke(new UpdateTextCallback(SetText), new object[] { "Pathping to " + ipAddress + " over a maximum of hops:" + maxHops + "\r\n" });
for (int i = 1; i < maxHops + 1; i++)
{
long tempo = 0;
int error = 0;
textBox2.BeginInvoke(new UpdateTextCallback(SetText), new object[] { i + ": " });
//trovo l' indirizzo IP numero n tramite il TTL
PingReply pingReply = pingSender.Send(ipAddress,5000, buffer, pingOptions);
//lo scrivo su una label
label1.BeginInvoke(new UpdateLabel(SetLabel1), new object[] { ipAddress.ToString() });
//per tot volte faccio il ping verso l' indirizzo trovato sopra
for (int k = 0; k < N_test; k++)
{
System.Threading.Thread.Sleep(100);
//Qui il problema che si presenta che richiama la funzione sotto !!!!!!!!!! <<<<<<----------------
progressBar1.Invoke(new BarDelegate(UpdateBar));
PingReply pingReply1 = pingSender.Send(pingReply.Address, 3000, buffer, pingOptions1);
if (!object.Equals(pingReply1.Status,IPStatus.Success))
{
error++;
}
textBox3.BeginInvoke(new UpdateTextCallback(SetText2), new object[] { k + " verso ip " + pingReply1.Address + " tempo " + pingReply1.RoundtripTime + " ms ttl: " + pingOptions.Ttl + " stato " + pingReply1.Status + "\r\n" });
tempo = tempo + pingReply1.RoundtripTime;
System.Threading.Thread.Sleep(500);
}
textBox2.BeginInvoke(new UpdateTextCallback(SetText), new object[] { " tempo medio " + (tempo / N_test) + "ms da indirizzo " + pingReply.Address + " pacchetti persi " + error + "\r\n" });
if (pingReply.Address.ToString() == ipAddress.ToString())
{
break;
}
pingOptions.Ttl++;
}
}
if (checkBox4.Checked)
{
download();
}
}
ed ecco qui la funzione richiamata:
codice:
private void UpdateBar()
{
progressBar1.Value++;
}