在获取局渔网主机IP的程序中,
在获取局渔网主机IP的程序中,
我创造了2个线程,但是出现了意想不到的问题,代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net ;
using System.Net .Sockets ;
using System.Threading ;
namespace 获取局域网IP
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Label label1;
string myIP;
Thread SearchIP1;
Thread SearchIP2;
private System.Windows.Forms.Timer timer1;
IPHostEntry IPhost;
int i;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
IPhost=Dns.GetHostByName (Dns.GetHostName());
myIP=IPhost.AddressList [0].ToString ();
myIP=myIP.Remove (myIP.LastIndexOf (".")+1,myIP.Length -myIP.LastIndexOf (".")-1);
timer1.Start ();
i=0;
SearchIP1=new Thread(new ThreadStart(this.SearchIP1_Threading));
SearchIP2=new Thread(new ThreadStart(this.SearchIP2_Threading));
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 34);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(272, 232);
this.listBox1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 1;
this.label1.Text = "局域网中的主机:";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 10;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.listBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
protected void SearchIP1_Threading()
{
for(int i=0;i<11;i++)
{
try
{
IPHostEntry eachIP=Dns.GetHostByAddress (myIP+i.ToString ());
listBox1.Items .Add ("IP地址:"+myIP+i.ToString ()+"主机名:"+eachIP.HostName.ToString ()+"\r\n");
}
catch(SocketException sxp)
{
//listBox1.Items .Add ("IP地址:"+myIP+i.ToString ()+"解析错误可能主机不在线!");
}
}
SearchIP1.Abort();
}
protected void SearchIP2_Threading()
{
for(int i=11;i<256;i++)
{
try
{
IPHostEntry eachIP=Dns.GetHostByAddress (myIP+i.ToString ());
listBox1.Items .Add ("IP地址:"+myIP+i.ToString ()+"主机名:"+eachIP.HostName.ToString ()+"\r\n");
}
catch(SocketException sxp)
{
//listBox1.Items .Add ("IP地址:"+myIP+i.ToString ()+"解析错误可能主机不在线!");
}
}
SearchIP2.Abort();
}
private void Form1_Load(object sender, System.EventArgs e)
{
SearchIP1.Start ();
SearchIP2.Start ();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
SearchIP1.Abort();
SearchIP2.Abort();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
}
}
}
你能帮我解决吗?盼回复!!!!