Autor Beitrag
basti2005
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: So 01.08.10 22:01 
Habe mir überlegt ein Tool zu schreiben was mit einem Ping die gegenstelle auf erreichbarkeit prüft.
Habe da auch nen Quellcode im Internet gefunden denn ich nur noch ändern muss.
Das tool funktioniert so weit recht gut.
Was ich gerne noch ändern würde das es eine ständige Prüfung stattfindet, also einen Dauerping und dem entsprechend seinen Satus ändert.

Geschrieben ist das alles in C# in Visual Studio 2010

Mein Problem ist jetzt das ich den eigentlichen Ping vorgang in ein Hintergrund-prozess legen muss da sonst die form blockiert wird. Leider weiß ich nicht wie ich das machen soll, geschweige denn welche teile ich verschieben muss.



Hier mein Code der Form1.cs

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
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;
using System.Threading;  // Eingefügt





namespace ping
{
    public partial class Form1 : Form
    {
   


        public Form1()
        {
           InitializeComponent();
      //  } 
        
        

      //  private void backgroundworker1_doWork(object sender, DoWorkEventArgs e) // Eingefügt
      //  {
           // Thread.Sleep(1000);   // Eingefügt
        }

        private void button1_Click(object sender, EventArgs e)
        {



button1.Visible = false;  // Button verschwindet nach dem anklicken           
button2.Enabled = true ;



// 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:




            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 = 10000;
            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";

                
            }

              

       //  goto Ping;




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


        //}

         private void button3_Click(object sender, EventArgs e)
         {
            System.Windows.Forms.Application.Exit();
         }

         private void button2_Click(object sender, EventArgs e)
         {



         }

         private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
         {
           







         }

    


    

 

 
    
      
    }

    }





und hier der Form1.Designer

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
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.button3 = new System.Windows.Forms.Button();
            this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            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(12178);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(7523);
            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(15142);
            this.panel5.Name = "panel5";
            this.panel5.Size = new System.Drawing.Size(11521);
            this.panel5.TabIndex = 2;
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.label6.Location = new System.Drawing.Point(84);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(3713);
            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(1534);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(11521);
            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(64);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(3713);
            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(1561);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(11521);
            this.panel2.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.label3.Location = new System.Drawing.Point(84);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(3713);
            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(1588);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(11521);
            this.panel3.TabIndex = 3;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.label4.Location = new System.Drawing.Point(84);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(3713);
            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(15115);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(11521);
            this.panel4.TabIndex = 3;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.label5.Location = new System.Drawing.Point(84);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(3713);
            this.label5.TabIndex = 6;
            this.label5.Text = "Offline";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(129);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(4013);
            this.label1.TabIndex = 4;
            this.label1.Text = "Status:";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(17634);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(10020);
            this.textBox1.TabIndex = 5;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(17661);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(10020);
            this.textBox2.TabIndex = 6;
            // 
            // textBox3
            // 
            this.textBox3.Location = new System.Drawing.Point(17688);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(10020);
            this.textBox3.TabIndex = 7;
            // 
            // textBox4
            // 
            this.textBox4.Location = new System.Drawing.Point(176115);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(10020);
            this.textBox4.TabIndex = 8;
            // 
            // textBox5
            // 
            this.textBox5.Location = new System.Drawing.Point(176142);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(10020);
            this.textBox5.TabIndex = 9;
            // 
            // button2
            // 
            this.button2.Enabled = false;
            this.button2.Location = new System.Drawing.Point(93178);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(8323);
            this.button2.TabIndex = 10;
            this.button2.Text = "Ping Stoppen";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(214181);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(7523);
            this.button3.TabIndex = 11;
            this.button3.Text = "Ende";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // backgroundWorker1
            // 
            this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork_1);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(426259);
            this.Controls.Add(this.button3);
            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;
        private System.Windows.Forms.Button button3;
        private System.ComponentModel.BackgroundWorker backgroundWorker1;
    }
}



und hier der Program.cs


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Ping_Tool
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new ping.Form1());
        }
    }
}


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
gfoidl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 157
Erhaltene Danke: 19

Win XP
C#, Fortran 95 - Visual Studio
BeitragVerfasst: Mo 02.08.10 00:49 
Hallo,

siehe auch Ping Tool erstellen.


mfG Gü

_________________
Alle sagten, das geht nicht! Dann kam einer, der wusste das nicht - und hat's gemacht!
CSMN
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 43
Erhaltene Danke: 2



BeitragVerfasst: Mo 02.08.10 20:01 
hi,

es gibt verschiedene vorgehensweisen, du könntest es via timer, backgroundworker, thread oder delegate laufen lassen.
wenn es dir reicht dass alle paar sekunden der ping ausgelöst wird würde ich ein timer nehmen und im hauptthread bleiben - ist halt die frage was du eigentlich erreichen möchtest!?