Ich bin gerade dabei ein Programm zuschreiben welches ein eine IP addresse einliest. Auf diese IP sollen 5 Ping geschickt werden. Nachdem Ersten Ping soll dem Benutzer Gezeigt werden (Mit einer TextBox die Rot oder Grün wird) ob der Ping erfolgreich war…dann soll der Zweite Ping ausgeführt werden und dann soll in der zweiten TextBox wieder angezeigt werden ob der Ping erfolgreich war…insgesamt gibt es 5pings mit 5 Textboxen…
Das Programm Funktioniert auch…ABER wenn ich es bewege bleibt das Programm hängen…
also habe ich mal rumgeguckt…und mir ist der BackgroundWorker ins augegefallen…
habe meine Pings in den BackgroundWorker eingefügt…
das Problem ist…die try catch funktion läuft solange bis was im fenster angezeigt werden soll…also bis zum ersten ping wo er dann grün bzw. rot werden soll…dann bricht sie ab und geht in die catch funktion.
hier mal mein code
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.NetworkInformation;
namespace PingTool
{
public partial class Form1 : Form
{
String add\_1 = "";
String status1 = "ok";
long p1\_1, p1\_2, p1\_3, p1\_4, p1\_5, p1\_g;
int sleeptime = 2000;
int maxtime = 50;
BackgroundWorker bw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.DoWork +=
new DoWorkEventHandler(bw\_DoWork);
bw.ProgressChanged +=new ProgressChangedEventHandler(bw\_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw\_RunWorkerCompleted);
}
public void ping1()
{
if (checkBox1.Checked == true)
{
add\_1 = textBox1.Text;
try
{
int errorcunt1 = 0;
Ping p1 = new Ping();
PingOptions po1 = new PingOptions();
//PingReply pingReply1 = p1.Send(add\_1);
ad\_p1\_1.BackColor = Color.White;
ad\_p1\_1.Text = "";
// this.Refresh();
PingReply pingReply1\_1 = p1.Send(add\_1);
p1\_1 = pingReply1\_1.RoundtripTime;
if (pingReply1\_1.Status.ToString() != "Success")
{
errorcunt1++;
ad\_p1\_1.BackColor = Color.Red;
ad\_p1\_1.Text = "-";
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
else
{
ad\_p1\_1.BackColor = Color.Green;
ad\_p1\_1.Text = p1\_1.ToString();
ad\_p1\_u.Text = DateTime.Now.ToString();
//this.Refresh();
}
ad\_p1\_2.BackColor = Color.White;
ad\_p1\_2.Text = "";
// this.Refresh();
System.Threading.Thread.Sleep(sleeptime);
PingReply pingReply1\_2 = p1.Send(add\_1);
p1\_2 = pingReply1\_2.RoundtripTime;
if (pingReply1\_2.Status.ToString() != "Success")
{
errorcunt1++;
ad\_p1\_2.BackColor = Color.Red;
ad\_p1\_2.Text = "-";
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
else
{
ad\_p1\_2.BackColor = Color.Green;
ad\_p1\_2.Text = p1\_2.ToString();
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
ad\_p1\_3.BackColor = Color.White;
ad\_p1\_3.Text = "";
// this.Refresh();
System.Threading.Thread.Sleep(sleeptime);
PingReply pingReply1\_3 = p1.Send(add\_1);
p1\_3 = pingReply1\_3.RoundtripTime;
if (pingReply1\_3.Status.ToString() != "Success")
{
errorcunt1++;
ad\_p1\_3.BackColor = Color.Red;
ad\_p1\_3.Text = "-";
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
else
{
ad\_p1\_3.BackColor = Color.Green;
ad\_p1\_3.Text = p1\_3.ToString();
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
ad\_p1\_4.BackColor = Color.White;
ad\_p1\_4.Text = "";
//this.Refresh();
System.Threading.Thread.Sleep(sleeptime);
PingReply pingReply1\_4 = p1.Send(add\_1);
p1\_4 = pingReply1\_4.RoundtripTime;
if (pingReply1\_4.Status.ToString() != "Success")
{
errorcunt1++;
ad\_p1\_4.BackColor = Color.Red;
ad\_p1\_4.Text = "-";
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
else
{
ad\_p1\_4.BackColor = Color.Green;
ad\_p1\_4.Text = p1\_4.ToString();
ad\_p1\_u.Text = DateTime.Now.ToString();
//this.Refresh();
}
ad\_p1\_5.BackColor = Color.White;
ad\_p1\_5.Text = "";
//this.Refresh();
System.Threading.Thread.Sleep(sleeptime);
PingReply pingReply1\_5 = p1.Send(add\_1);
p1\_5 = pingReply1\_5.RoundtripTime;
if (pingReply1\_5.Status.ToString() != "Success")
{
errorcunt1++;
ad\_p1\_5.BackColor = Color.Red;
ad\_p1\_5.Text = "-";
ad\_p1\_u.Text = DateTime.Now.ToString();
// this.Refresh();
}
else
{
ad\_p1\_5.BackColor = Color.Green;
ad\_p1\_5.Text = p1\_5.ToString();
ad\_p1\_u.Text = DateTime.Now.ToString();
//this.Refresh();
}
p1\_g = (p1\_1 + p1\_2 + p1\_3 + p1\_4 + p1\_5) / 5;
if ((errorcunt1 =2) && (((p1\_g == 0) || p1\_g \>= maxtime)))
{
if (status1 == "ok")
{
System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient();
mail.Host = "IP";
mail.Send("[email protected]", "[email protected]", "Fehlermeldung PingTool", "Die Adresse " + add\_1 + " hat eine Fehlermeldung! Bitte Überprüfen\nDurchschnittantwortzeit beträgt: " + p1\_g + "ms");
status1 = "gesendet";
}
}
}
catch
{
if (status1 == "ok")
{
System.Net.Mail.SmtpClient mail = new System.Net.Mail.SmtpClient();
mail.Host = "IP";
mail.Send("[email protected]", "[email protected]", "Fehlermeldung PingTool", "Die Adresse " + add\_1 + " hat eine Fehlermeldung! Bitte Überprüfen\nDurchschnittantwortzeit beträgt: " + p1\_g + "ms");
status1 = "gesendet";
}
}
}
}
private void button1\_Click(object sender, EventArgs e)
{
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
}
private void button2\_Click(object sender, EventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
{
bw.CancelAsync();
}
}
private void bw\_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
ping1();
}
public void bw\_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}
private void bw\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.tbProgress.Text = "Canceled!";
}
else if (!(e.Error == null))
{
this.tbProgress.Text = ("Error: " + e.Error.Message);
}
else
{
this.tbProgress.Text = "Done!";
}
}
vllt kann mir ja jemand helfen…
vielen dank im vorraus