网上 说法是用 委托 可是这个已经用了委托 还是不行 希望大侠帮个忙 告诉个解决办法后台代码 前台就是一个LISTBOX 主要是一个PING类 用于接受线程里面的IPusing System;
using System.Management;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using System.Threading;namespace WindowsApplication2
{ public delegate void UpdataList(string sip, string shostname); public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void UpDataMyList(string sip, string shostname)
{ lock (this.listView1)
{
if (shostname != "no")
{
listBox1.Items.Add("扫描" + sip + shostname);
}
else
{
listBox1.Items.Add(sip + "不存在");
}
}
} private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
int min = 1;
int max = 10;
string mask = "192.168.0.";
int threadNum = max - min + 1;
Thread[] myThread = new Thread[threadNum];
for (int i = min; i <= max; i++)
{
int k = max - i;
Ping myping = new Ping();
myping.ip=mask+i.ToString();
myping.ul = new UpdataList(UpDataMyList);
myThread[k] = new Thread(new ThreadStart(myping.sacn));
myThread[k].Start(); }
} } public class Ping
{
public UpdataList ul; public string ip;
public string shostname; public void sacn()
{
IPAddress myip = IPAddress.Parse(ip);
try
{
IPHostEntry myhost = Dns.GetHostByAddress(myip);
shostname = myhost.HostName.ToString();
}
catch
{
shostname = "";
} if (shostname == "")
shostname = "no";
if (ul != null)
ul(ip, shostname);
} }
}
using System.Management;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Net;
using System.Threading;namespace WindowsApplication2
{ public delegate void UpdataList(string sip, string shostname); public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void UpDataMyList(string sip, string shostname)
{ lock (this.listView1)
{
if (shostname != "no")
{
listBox1.Items.Add("扫描" + sip + shostname);
}
else
{
listBox1.Items.Add(sip + "不存在");
}
}
} private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
int min = 1;
int max = 10;
string mask = "192.168.0.";
int threadNum = max - min + 1;
Thread[] myThread = new Thread[threadNum];
for (int i = min; i <= max; i++)
{
int k = max - i;
Ping myping = new Ping();
myping.ip=mask+i.ToString();
myping.ul = new UpdataList(UpDataMyList);
myThread[k] = new Thread(new ThreadStart(myping.sacn));
myThread[k].Start(); }
} } public class Ping
{
public UpdataList ul; public string ip;
public string shostname; public void sacn()
{
IPAddress myip = IPAddress.Parse(ip);
try
{
IPHostEntry myhost = Dns.GetHostByAddress(myip);
shostname = myhost.HostName.ToString();
}
catch
{
shostname = "";
} if (shostname == "")
shostname = "no";
if (ul != null)
ul(ip, shostname);
} }
}
myping.ul = new UpdataList(UpDataMyList); 作为一个变量了
public void UpDataMyList(string sip, string shostname)
{
if (this.InvokeRequired)
{
this.Invoke(new UpdataList(UpDataMyList), new object[] { sip, shostname });
}
else
{ lock (this.listView1)
{
if (shostname != "no")
{
listBox1.Items.Add("扫描" + sip + shostname); }
else
{
listBox1.Items.Add(sip + "不存在");
}
}
} }