#1 30. Juli 2010 Ping Tool erstellen hi habe im Internet einen schönen Quelltext gefunden der sehr gut zu meinem vorhaben passt Das programm ping 5 adressen an und gibt dann den Status aus. Ich möchte ihn so ändern das er dauern kontrolliert ob das Ziel erreichbar ist. Man aber jederzeit das Programm beenden kann. Geschrieben wurde das Programm in C# benutze zum coden Visual Studio 2010 hier der Quellcode der Form.cs 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 ping { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { button1.Visible = false ; // Button verschwindet nach dem anklicken // Hier kann man die Adressen eintragen an die er pingen soll //----------------------------------------------------------------- if (textBox1.Text == "") { textBox1.Text = "www.winxperts.net"; } if (textBox2.Text == "") { textBox2.Text = "www.google.at"; } if (textBox3.Text == "") { textBox3.Text = "www.winfuture.de"; } if (textBox4.Text == "") { textBox4.Text = "www.aeroxp.org"; } if (textBox5.Text == "") { textBox5.Text = "www.joejoe.org"; } //----------------------------------------------------------------- Ping myPing = new Ping(); String host = textBox1.Text; String host2 = textBox2.Text; String host3 = textBox3.Text; String host4 = textBox4.Text; String host5 = textBox5.Text; byte[] buffer = new byte[32]; int timeout = 1000; PingOptions pingOptions = new PingOptions(); PingReply reply = myPing.Send(host, timeout, buffer, pingOptions); PingReply reply2 = myPing.Send(host2, timeout, buffer, pingOptions); PingReply reply3 = myPing.Send(host3, timeout, buffer, pingOptions); PingReply reply4 = myPing.Send(host4, timeout, buffer, pingOptions); PingReply reply5 = myPing.Send(host5, timeout, buffer, pingOptions); if (reply.Status == IPStatus.Success) { panel1.BackColor = System.Drawing.Color.Green; label2.Text = "Online"; } else if (reply.Status == IPStatus.TimedOut) { panel1.BackColor = System.Drawing.Color.Yellow; label2.Text = Convert.ToString(reply.Status); } else if (reply.Status == IPStatus.BadDestination || reply.Status == IPStatus.BadOption) { panel1.BackColor = System.Drawing.Color.Maroon; label2.Text = Convert.ToString(reply.Status); } if (reply2.Status == IPStatus.Success) { panel2.BackColor = System.Drawing.Color.Green; label3.Text = "Online"; } else if (reply2.Status == IPStatus.TimedOut) { panel2.BackColor = System.Drawing.Color.Yellow; label3.Text = "Timeout"; } else if (reply2.Status == IPStatus.BadDestination || reply2.Status == IPStatus.BadOption) { panel2.BackColor = System.Drawing.Color.Maroon; label3.Text = "404"; } if (reply3.Status == IPStatus.Success) { panel3.BackColor = System.Drawing.Color.Green; label4.Text = "Online"; } else if (reply3.Status == IPStatus.TimedOut) { panel3.BackColor = System.Drawing.Color.Yellow; label4.Text = "Timeout"; } else if (reply3.Status == IPStatus.BadDestination || reply3.Status == IPStatus.BadOption) { panel3.BackColor = System.Drawing.Color.Maroon; label4.Text = "404"; } if (reply4.Status == IPStatus.Success) { panel4.BackColor = System.Drawing.Color.Green; label5.Text = "Online"; } else if (reply4.Status == IPStatus.TimedOut) { panel4.BackColor = System.Drawing.Color.Yellow; label5.Text = "Timeout"; } else if (reply4.Status == IPStatus.BadDestination || reply4.Status == IPStatus.BadOption) { panel4.BackColor = System.Drawing.Color.Maroon; label5.Text = "404"; } if (reply5.Status == IPStatus.Success) { panel5.BackColor = System.Drawing.Color.Green; label6.Text = "Online"; } else if (reply5.Status == IPStatus.TimedOut) { panel5.BackColor = System.Drawing.Color.Yellow; label6.Text = "Timeout"; } else if (reply5.Status == IPStatus.BadDestination || reply5.Status == IPStatus.BadOption) { panel5.BackColor = System.Drawing.Color.Maroon; label6.Text = "404"; } button1.Visible = true; // Nach dem Pingen erscheint der Button wieder } private void Form1_Load(object sender, EventArgs e) { } private void panel1_Paint(object sender, PaintEventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { //button1.Visible = true; // Button verschwindet nach dem anklicken //button2.Visible = false; // Button verschwindet nach dem anklicken } } } und der Code von der Form1.Designer.cs Code: namespace ping { partial class Form1 { /// <summary> /// Erforderliche Designervariable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Verwendete Ressourcen bereinigen. /// </summary> /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Vom Windows Form-Designer generierter Code /// <summary> /// Erforderliche Methode für die Designerunterstützung. /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.panel5 = new System.Windows.Forms.Panel(); this.label6 = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); this.label2 = new System.Windows.Forms.Label(); this.panel2 = new System.Windows.Forms.Panel(); this.label3 = new System.Windows.Forms.Label(); this.panel3 = new System.Windows.Forms.Panel(); this.label4 = new System.Windows.Forms.Label(); this.panel4 = new System.Windows.Forms.Panel(); this.label5 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.textBox5 = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.panel5.SuspendLayout(); this.panel1.SuspendLayout(); this.panel2.SuspendLayout(); this.panel3.SuspendLayout(); this.panel4.SuspendLayout(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 178); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Ping Starten"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // panel5 // this.panel5.BackColor = System.Drawing.Color.Red; this.panel5.Controls.Add(this.label6); this.panel5.Location = new System.Drawing.Point(15, 142); this.panel5.Name = "panel5"; this.panel5.Size = new System.Drawing.Size(115, 21); this.panel5.TabIndex = 2; // // label6 // this.label6.AutoSize = true; this.label6.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.label6.Location = new System.Drawing.Point(8, 4); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(37, 13); this.label6.TabIndex = 6; this.label6.Text = "Offline"; // // panel1 // this.panel1.BackColor = System.Drawing.Color.Red; this.panel1.Controls.Add(this.label2); this.panel1.Location = new System.Drawing.Point(15, 34); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(115, 21); this.panel1.TabIndex = 3; this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint); // // label2 // this.label2.AutoSize = true; this.label2.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.label2.Location = new System.Drawing.Point(6, 4); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(37, 13); this.label2.TabIndex = 5; this.label2.Text = "Offline"; // // panel2 // this.panel2.BackColor = System.Drawing.Color.Red; this.panel2.Controls.Add(this.label3); this.panel2.Location = new System.Drawing.Point(15, 61); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(115, 21); this.panel2.TabIndex = 3; // // label3 // this.label3.AutoSize = true; this.label3.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.label3.Location = new System.Drawing.Point(8, 4); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(37, 13); this.label3.TabIndex = 6; this.label3.Text = "Offline"; // // panel3 // this.panel3.BackColor = System.Drawing.Color.Red; this.panel3.Controls.Add(this.label4); this.panel3.Location = new System.Drawing.Point(15, 88); this.panel3.Name = "panel3"; this.panel3.Size = new System.Drawing.Size(115, 21); this.panel3.TabIndex = 3; // // label4 // this.label4.AutoSize = true; this.label4.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.label4.Location = new System.Drawing.Point(8, 4); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(37, 13); this.label4.TabIndex = 6; this.label4.Text = "Offline"; // // panel4 // this.panel4.BackColor = System.Drawing.Color.Red; this.panel4.Controls.Add(this.label5); this.panel4.Location = new System.Drawing.Point(15, 115); this.panel4.Name = "panel4"; this.panel4.Size = new System.Drawing.Size(115, 21); this.panel4.TabIndex = 3; // // label5 // this.label5.AutoSize = true; this.label5.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.label5.Location = new System.Drawing.Point(8, 4); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(37, 13); this.label5.TabIndex = 6; this.label5.Text = "Offline"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 9); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(40, 13); this.label1.TabIndex = 4; this.label1.Text = "Status:"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(176, 34); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 5; this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); // // textBox2 // this.textBox2.Location = new System.Drawing.Point(176, 61); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 20); this.textBox2.TabIndex = 6; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(176, 88); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(100, 20); this.textBox3.TabIndex = 7; // // textBox4 // this.textBox4.Location = new System.Drawing.Point(176, 115); this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(100, 20); this.textBox4.TabIndex = 8; // // textBox5 // this.textBox5.Location = new System.Drawing.Point(176, 142); this.textBox5.Name = "textBox5"; this.textBox5.Size = new System.Drawing.Size(100, 20); this.textBox5.TabIndex = 9; // // button2 // this.button2.Location = new System.Drawing.Point(93, 178); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(83, 23); this.button2.TabIndex = 10; this.button2.Text = "Ping Stoppen"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(301, 216); this.Controls.Add(this.button2); this.Controls.Add(this.textBox5); this.Controls.Add(this.textBox4); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Controls.Add(this.panel3); this.Controls.Add(this.panel1); this.Controls.Add(this.panel2); this.Controls.Add(this.panel4); this.Controls.Add(this.panel5); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Ping"; this.Load += new System.EventHandler(this.Form1_Load); this.panel5.ResumeLayout(false); this.panel5.PerformLayout(); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); this.panel2.ResumeLayout(false); this.panel2.PerformLayout(); this.panel3.ResumeLayout(false); this.panel3.PerformLayout(); this.panel4.ResumeLayout(false); this.panel4.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Panel panel5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Panel panel3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Panel panel4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.TextBox textBox5; private System.Windows.Forms.Button button2; } } + Multi-Zitat Zitieren
#2 30. Juli 2010 AW: Ping Tool erstellen Wenn du selbst keine Kenntnisse in C# besitzt, um das Programm selbst zu schreiben, dann benutze bitte den "Ich-Suche-Programmierer-Thread". Ansonsten poste doch erstmal deinen Ansatz, was du bereits versucht hast etc. Dann kann man dir auch gezielter weiterhelfen. + Multi-Zitat Zitieren
#4 30. Juli 2010 AW: Ping Tool erstellen Ja schön, aber du hast weder eine Frage gestellt noch um Hilfe gebeten. Was erwartest du denn nun von uns?! + Multi-Zitat Zitieren
#5 30. Juli 2010 AW: Ping Tool erstellen Doch steht doch da , das ich das so ändern das es stänig kontrolliert ob die adresse erreichbar ist und dann halt entsprechen den status ändert (was ja schon funktioniert). Es muss warscheinlich nur ander richtigen stelle ein e schleife eingefügt werden. Habe es schon mal versuchsweise mit goto versucht ist zwar unsauber war aber auch erstmal nur zum testen. das zweite was ich ergänzen will ist, das ich halt das programm unterbrechen kann , was mit dem goto nicht funktionierte. + Multi-Zitat Zitieren
#6 30. Juli 2010 AW: Ping Tool erstellen Das mit der Schleife stimmt. Du setzt einfach den Teil, der wiederholt werden soll in eine Schleife. Als Abbruchbedingung fragst du eine Variable ab. Wenn abgebrochen werden soll, dann setzt du die Variable in der entsprechenden Funktion auf einen bestimmten Wert. Dadurch wird die Schleife unterbrochen und das Programm kann beendet werden. + Multi-Zitat Zitieren
#7 30. Juli 2010 AW: Ping Tool erstellen Genau das ist das Problem habe ja mit der goto Anweisung probiert. In der zeit wo das programm läuft also ständig in der "go to schleife" hängt kann ich das Programm nur übern TaskManager abschießen. + Multi-Zitat Zitieren
#8 30. Juli 2010 AW: Ping Tool erstellen Les dich am besten mal in "Threads" ein. Dann lagerst du das Abfragen in einen andern Thread aus. Dadurch wird der Thread in dem die Form läuft nicht blockiert. Invoke und Delegates werden wohl auch hilfreich sein wenn du versuchst die Form aus dem "ausgelagerten" Thread zubearbeiten. + Multi-Zitat Zitieren
#9 31. Juli 2010 AW: Ping Tool erstellen Probier es mal mit einem Timer. Ist leider schon eine weile her wo ich damit gerarbeitet hab, daher googel einfach mal nach der funktion"SetTimer()". Da kannste die funktion zum pingen in einen Timer packen und du kannst angeben, in welchen Zeitraum sich die funktion wiederholt aufrufen soll. + Multi-Zitat Zitieren
#10 31. Juli 2010 AW: Ping Tool erstellen Threads werden hier nicht viel helfen, aber ein Timer ist auf jeden Fall ratsam. Den kannst du dir einfach als Steuerelment auf deine Form ziehen und da dann bestimmen welche Aktion in welchen Abständen ausgeführt werden. Natürlich kannst du das mit nem BackgroundWorker oder einem Thread machen, aber eine Schleife würde ich nicht nehmen, da diese in MainThread die ganze Form blockieren würde. Hier noch Links zu den genannten Themen: Timer BackgroundWorker greez + Multi-Zitat Zitieren
#11 31. Juli 2010 Zuletzt von einem Moderator bearbeitet: 14. April 2017 AW: Ping Tool erstellen Ich mag mich täuschen, aber ein Timer ist doch nichts anderes als ein rudimentärer Thread? Man könnte genauso mit Threads und Sleep arbeiten. In dem Sinne helfen Threads genauso wie irgendwie Timer. Wegen der Sache mit der Form, sehe ich keine Probleme, wenn man Threads nimmt. //Hab mal was in VB.NET gemacht, lässt sich leicht in C# übersetzen: No File | www.xup.in Das ist meiner Meinung nach die beste Struktur zur Lösung deines Problems. Es treten so auch keine Konflikte mit der GUI o.ä. auf. Die Rückmeldung der Threads könntest du über Events machen. + Multi-Zitat Zitieren
#12 31. Juli 2010 AW: Ping Tool erstellen Das mit dem Backgroundworker scheint die einfache Lösung zu sein. Habe das ebend auch schon mal probiert nur funktionieren dann einige sachen nicht mehr. So wie ich das sehe muss ich den ganzen code bzw den größteb teil in den Backgroundworker verschieben oder ? + Multi-Zitat Zitieren
#13 31. Juli 2010 AW: Ping Tool erstellen du solltest den kompletten pingversuch in den thread/backgroundworker verschieben so würde ich es machen + Multi-Zitat Zitieren
#14 31. Juli 2010 AW: Ping Tool erstellen muss ich den Backgrounder eigentlich an einer bestimmten stelle plazieren ? Welchen Teil des Codes würdest ihr in den Backgrounder schieben? + Multi-Zitat Zitieren
#15 31. Juli 2010 AW: Ping Tool erstellen Du hast wirklich gar nichts verstanden . Wieso versuchst du nicht erstmal die Grundlagen zu erlernen bevor du hier überflüssige Fragen stellst?! + Multi-Zitat Zitieren