How can i stop my ping?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I ask this question for 1 simple reason...... i have a section of my app that pings ip address or web addresses...and if i click the "x" on the form to close it....it throws an error....so i need a cancel button to cancel the ping prior to closing the form....any
ideas? check the code that i have to see if we can impliment something here for a button1 click to stop the ping prior to exiting the form....and i also i want to add a message telling the user that the ping was "successfull" so if you can see in my code where
i can put this message at that would be super helpfull too. Please only respond with C# code thanks!!
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.ComponentModel;
<span style="color:Blue; using System.Data;
<span style="color:Blue; using System.Drawing;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Windows.Forms;
<span style="color:Blue; using System.Threading;
<span style="color:Blue; using System.Net.NetworkInformation;
<span style="color:Blue; using System.Runtime.InteropServices;

<span style="color:Blue; namespace Access_Honeywell_V8
{


<span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class ping : Form
{
<span style="color:Green; //Constants
<span style="color:Blue; const <span style="color:Blue; int AW_SLIDE = 0X40000;
<span style="color:Blue; const <span style="color:Blue; int AW_HOR_POSITIVE = 0X1;
<span style="color:Blue; const <span style="color:Blue; int AW_HOR_NEGATIVE = 0X2;
<span style="color:Blue; const <span style="color:Blue; int AW_BLEND = 0X80000;
<span style="color:Blue; const <span style="color:Blue; int AW_HIDE = 0x1;
[DllImport(<span style="color:#A31515; "user32")]
<span style="color:Blue; static <span style="color:Blue; extern <span style="color:Blue; bool AnimateWindow(IntPtr hwnd, <span style="color:Blue; int time, <span style="color:Blue; int flags);

<span style="color:Green; // Counts the pings
<span style="color:Blue; private <span style="color:Blue; int pingsSent;
<span style="color:Green; // Can be used to notify when the operation completes
AutoResetEvent resetEvent = <span style="color:Blue; new AutoResetEvent(<span style="color:Blue; false);
<span style="color:Green; // this is where i receive the data from the extrasiteinfo form

<span style="color:Blue; public ping(TextBox t)
{
InitializeComponent();
<span style="color:Blue; this.txtIP.Text = t.Text;
}

<span style="color:Blue; public ping()
{
InitializeComponent();
}


<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void OnLoad(EventArgs e)
{
<span style="color:Green; //Load the Form At Position of Main Form
<span style="color:Blue; int WidthOfMain = Application.OpenForms[<span style="color:#A31515; "MainForm"].Width;
<span style="color:Blue; int HeightofMain = Application.OpenForms[<span style="color:#A31515; "MainForm"].Height;
<span style="color:Blue; int LocationMainX = Application.OpenForms[<span style="color:#A31515; "MainForm"].Location.X;
<span style="color:Blue; int locationMainy = Application.OpenForms[<span style="color:#A31515; "MainForm"].Location.Y;

<span style="color:Green; //Set the Location
<span style="color:Blue; this.Location = <span style="color:Blue; new Point(LocationMainX + WidthOfMain, locationMainy + 1);

<span style="color:Green; //Animate form
AnimateWindow(<span style="color:Blue; this.Handle, 500, AW_SLIDE | AW_HOR_POSITIVE);
}

<span style="color:Blue; private <span style="color:Blue; void btnPing_Click(<span style="color:Blue; object sender, EventArgs e)
{
<span style="color:Green; // Reset the number of pings
pingsSent = 0;
<span style="color:Green; // Clear the textbox of any previous content
txtResponse.Clear();
txtResponse.Text += <span style="color:#A31515; "Pinging " + txtIP.Text + <span style="color:#A31515; " with 32 bytes of data:rnrn";
<span style="color:Green; // Send the ping
SendPing();
}

<span style="color:Blue; private <span style="color:Blue; void SendPing()
{
System.Net.NetworkInformation.Ping pingSender = <span style="color:Blue; new System.Net.NetworkInformation.Ping();

<span style="color:Green; // Create an event handler for ping complete
pingSender.PingCompleted += <span style="color:Blue; new PingCompletedEventHandler(pingSender_Complete);

<span style="color:Green; // Create a buffer of 32 bytes of data to be transmitted.
<span style="color:Blue; byte[] packetData = Encoding.ASCII.GetBytes(<span style="color:#A31515; "................................");

<span style="color:Green; // Jump though 50 routing nodes tops, and dont fragment the packet
PingOptions packetOptions = <span style="color:Blue; new PingOptions(50, <span style="color:Blue; true);

<span style="color:Green; // Send the ping asynchronously
pingSender.SendAsync(txtIP.Text, 5000, packetData, packetOptions, resetEvent);
}

<span style="color:Blue; private <span style="color:Blue; void pingSender_Complete(<span style="color:Blue; object sender, PingCompletedEventArgs e)
{
<span style="color:Green; // If the operation was canceled, display a message to the user.
<span style="color:Blue; if (e.Cancelled)
{
txtResponse.Text += <span style="color:#A31515; "Ping was canceled...rn";

<span style="color:Green; // The main thread can resume
((AutoResetEvent)e.UserState).Set();
}
<span style="color:Blue; else <span style="color:Blue; if (e.Error != <span style="color:Blue; null)
{
txtResponse.Text += <span style="color:#A31515; "An error occured: " + e.Error + <span style="color:#A31515; "rn";

<span style="color:Green; // The main thread can resume
((AutoResetEvent)e.UserState).Set();
}
<span style="color:Blue; else
{
PingReply pingResponse = e.Reply;
<span style="color:Green; // Call the method that displays the ping results, and pass the information with it
ShowPingResults(pingResponse);
}
}

<span style="color:Blue; public <span style="color:Blue; void ShowPingResults(PingReply pingResponse)
{
<span style="color:Blue; if (pingResponse == <span style="color:Blue; null)
{
<span style="color:Green; // We got no response
txtResponse.Text += <span style="color:#A31515; "There was no response.rnrn";
<span style="color:Blue; return;
}
<span style="color:Blue; else <span style="color:Blue; if (pingResponse.Status == IPStatus.Success)
{
<span style="color:Green; // We got a response, lets see the statistics
txtResponse.Text += <span style="color:#A31515; "Reply from " + pingResponse.Address.ToString() + <span style="color:#A31515; ": bytes=" + pingResponse.Buffer.Length + <span style="color:#A31515; " time=" + pingResponse.RoundtripTime + <span style="color:#A31515; " TTL=" + pingResponse.Options.Ttl + <span style="color:#A31515; "rn";
}
<span style="color:Blue; else
{
<span style="color:Green; // The packet didnt get back as expected, explain why
txtResponse.Text += <span style="color:#A31515; "Ping was unsuccessful: " + pingResponse.Status + <span style="color:#A31515; "rnrn";
}
<span style="color:Green; // Increase the counter so that we can keep track of the pings sent
pingsSent++;
<span style="color:Green; // Send 4 pings
<span style="color:Blue; if (pingsSent < 4)
{
SendPing();
}
}
}
}
[/code]
So....i want to click a button to "stop" the ping before exiting the form....how? and 2. i want to add a message in the process that says "ping successfull" or "connection successfull"
How do i do this? Thanks in advance!! <hr class="sig Preston Lambeth

View the full article
 
Back
Top