在单击按钮关闭表单之前,我需要停止对服务器进行ping操作...我该怎么做? [英] I need to stop pinging my server before closing my form with a button click...how do i do this?
问题描述
我问这个问题的原因很简单……我的应用程序中有一个部分可以ping ip地址或网址...并且如果我单击"x"按钮,在窗体上关闭它....它引发一个错误....所以我需要一个取消按钮来取消ping,然后再关闭窗体..任何 想法?检查代码,我必须查看我们是否可以在此处为button1隐含某些内容.在退出表单之前单击以停止ping....我还想添加一条消息,告诉用户ping是成功"的. ;因此,如果您可以在我的代码中看到 我可以把这条信息放在那也将是超级有帮助的.请仅以C#代码回复!谢谢!
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!!
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.Threading;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
namespace Access_Honeywell_V8
{
public partial class ping : Form
{
//Constants
const int AW_SLIDE = 0X40000;
const int AW_HOR_POSITIVE = 0X1;
const int AW_HOR_NEGATIVE = 0X2;
const int AW_BLEND = 0X80000;
const int AW_HIDE = 0x1;
[DllImport("user32")]
static extern bool AnimateWindow(IntPtr hwnd, int time, int flags);
// Counts the pings
private int pingsSent;
// Can be used to notify when the operation completes
AutoResetEvent resetEvent = new AutoResetEvent(false);
// this is where i receive the data from the extrasiteinfo form
public ping(TextBox t)
{
InitializeComponent();
this.txtIP.Text = t.Text;
}
public ping()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
//Load the Form At Position of Main Form
int WidthOfMain = Application.OpenForms["MainForm"].Width;
int HeightofMain = Application.OpenForms["MainForm"].Height;
int LocationMainX = Application.OpenForms["MainForm"].Location.X;
int locationMainy = Application.OpenForms["MainForm"].Location.Y;
//Set the Location
this.Location = new Point(LocationMainX + WidthOfMain, locationMainy + 1);
//Animate form
AnimateWindow(this.Handle, 500, AW_SLIDE | AW_HOR_POSITIVE);
}
private void btnPing_Click(object sender, EventArgs e)
{
// Reset the number of pings
pingsSent = 0;
// Clear the textbox of any previous content
txtResponse.Clear();
txtResponse.Text += "Pinging " + txtIP.Text + " with 32 bytes of data:\r\n\r\n";
// Send the ping
SendPing();
}
private void SendPing()
{
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
// Create an event handler for ping complete
pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);
// Create a buffer of 32 bytes of data to be transmitted.
byte[] packetData = Encoding.ASCII.GetBytes("................................");
// Jump though 50 routing nodes tops, and don't fragment the packet
PingOptions packetOptions = new PingOptions(50, true);
// Send the ping asynchronously
pingSender.SendAsync(txtIP.Text, 5000, packetData, packetOptions, resetEvent);
}
private void pingSender_Complete(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
txtResponse.Text += "Ping was canceled...\r\n";
// The main thread can resume
((AutoResetEvent)e.UserState).Set();
}
else if (e.Error != null)
{
txtResponse.Text += "An error occured: " + e.Error + "\r\n";
// The main thread can resume
((AutoResetEvent)e.UserState).Set();
}
else
{
PingReply pingResponse = e.Reply;
// Call the method that displays the ping results, and pass the information with it
ShowPingResults(pingResponse);
}
}
public void ShowPingResults(PingReply pingResponse)
{
if (pingResponse == null)
{
// We got no response
txtResponse.Text += "There was no response.\r\n\r\n";
return;
}
else if (pingResponse.Status == IPStatus.Success)
{
// We got a response, let's see the statistics
txtResponse.Text += "Reply from " + pingResponse.Address.ToString() + ": bytes=" + pingResponse.Buffer.Length + " time=" + pingResponse.RoundtripTime + " TTL=" + pingResponse.Options.Ttl + "\r\n";
}
else
{
// The packet didn't get back as expected, explain why
txtResponse.Text += "Ping was unsuccessful: " + pingResponse.Status + "\r\n\r\n";
}
// Increase the counter so that we can keep track of the pings sent
pingsSent++;
// Send 4 pings
if (pingsSent < 4)
{
SendPing();
}
}
}
}
推荐答案
我在这里猜测,但是这样做会释放资源,这可能会导致您在点击"X"时出错按钮?
I'm guessing here but would something like this work to release the resources which may be causing your error on hitting the 'X' button?
这是一篇有关异步发送时内存泄漏的文章:http://chicknet.blogspot.com/2006/11/ping-memory-leak.html
Here is an article on memory leaks while sending asynchronously: http://chicknet.blogspot.com/2006/11/ping-memory-leak.html
他们似乎建议将以下内容放在ping_completed回调的末尾.只是一个猜测.
They suggest, it seems, to put the following at the end of the ping_completed callback. Just a guess.
((IDisposable)pingSender).Dispose();
这篇关于在单击按钮关闭表单之前,我需要停止对服务器进行ping操作...我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!