当我启动程序时,表单会冻结,有时我会看到蓝屏并重新启动计算机。
我正在运行Windows 7 Enterprise 64并拥有Intel核心i5 2.60 GHz和8 GB RAM
如果有人知道问题是什么,那将是非常有用的,谢谢。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//IP variables
string localIP = GetLocalIPAddress();
string[] targetIP = new string[255];
//Init
System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
//Variables
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
try
{
for (int i = 0; i < 255; i++)
{
//textbox.text example: 10.20.18.
//i = 0 > 255
targetIP[i] = textBox2.Text + i;
//send ping
PingReply reply = pingSender.Send(targetIP[i], timeout, buffer, options);
//if reply is successfull
if (reply.Status == IPStatus.Success)
{
textBox1.AppendText("Address: " + reply.Address + "\t ms: " + reply.RoundtripTime + "\n");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("Local IP Address Not Found!");
}
}
答案 0 :(得分:0)
Ping对象是一次性的......所以用它来释放windows资源。
for (int i = 0; i < 255; i++)
{
using (System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping())
{
//textbox.text example: 10.20.18.
//i = 0 > 255
targetIP[i] = textBox2.Text + i;
//send ping
PingReply reply = pingSender.Send(targetIP[i], timeout, buffer, options);
//if reply is successfull
if (reply.Status == IPStatus.Success)
{
textBox1.AppendText("Address: " + reply.Address + "\t ms: " + reply.RoundtripTime + "\n");
}
}