110,534
社区成员
发帖
与我相关
我的任务
分享
public delegate void OnUiControl();
private void button1_Click(object sender, EventArgs e)
{
Thread thScan = new Thread(new ThreadStart(DoControl));
thScan.IsBackground = true;
thScan.Start();
}
private void DoControl()
{
OnUiControl uicontrol = this.ScanTarget;
if (this.InvokeRequired)
{
this.Invoke(uicontrol);
}
}
private void ScanTarget()
{
string strIPAddress = "172.30.81."; //ip段
int nStrat = Int32.Parse("2"); //开始扫描地址
int nEnd = Int32.Parse("50"); //终止扫描地址
//开始扫描(耗时操作)
for (int i = nStrat; i <= nEnd; i++)
{
string strScanIPAdd = strIPAddress + i.ToString();
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd); //转换成IP地址
try
{
//当执行下面这个操作时候比较耗时
IPHostEntry myScanHost = Dns.Resolve(strScanIPAdd);
string strHostName = myScanHost.HostName;
richTextBox1.AppendText(strHostName + "\r");
Application.DoEvents();
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
public partial class FrmTest : Form
{
private Action<IPHostEntry> _reportIP;
private int nStrat = 100, nEnd = 150; //扫描地址范围
private bool _asyCancellation = false;
public FrmTest()
{
InitializeComponent();
}
private void ScanTarget()
{
string strIPAddress = "192.168.0."; //ip段
//开始扫描(耗时操作)
for (int i = nStrat; i <= nEnd; i++)
{
if (this._asyCancellation) return;
string strScanIPAdd = strIPAddress + i.ToString();
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd); //转换成IP地址
try
{
//当执行下面这个操作时候比较耗时
IPHostEntry myScanHost = Dns.Resolve(strScanIPAdd);
if (this.InvokeRequired)
{
this.Invoke(this._reportIP, myScanHost);
}
else
{
this._reportIP(myScanHost);
}
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}
private void OnResolvedCallback(IPHostEntry ip)
{
this.richTextBox1.AppendText(ip.HostName + "\r");
//假设界面上还有一个进度条
var curIP = (int)ip.AddressList[0].GetAddressBytes().Last();
var rate = (curIP - nStrat) / (double)(nEnd - nStrat);
this.progressBar1.Value = Convert.ToInt32(rate * 100);
}
private void btnOK_Click(object sender, EventArgs e)
{
if (!this._asyCancellation)
{
if (this._reportIP == null) this._reportIP = this.OnResolvedCallback;
this.progressBar1.Value = this.progressBar1.Minimum;
Thread thScan = new Thread(new ThreadStart(ScanTarget));
thScan.IsBackground = true;
thScan.Start();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this._asyCancellation = true;
this.progressBar1.Value = this.progressBar1.Maximum;
}
}
直接用BackgroundWoker省事多了//回调处理
private Action<string> _reportIP;
private int nStrat = 2, nEnd = 50; //扫描地址范围
//----上面是成员变量,放在方法外----
private void button1_Click(object sender, EventArgs e)
{
//回调处理可移到Form_Load中,如果其他的Form要用到,最好封装到事件中去
this._reportIP = ip =>
{
this.richTextBox1.AppendText(ip + "\r");
//假设界面上还有一个进度条
int curIP = int.Parse(ip.Split(".").Last());
this.progessBar1.Value = (curIP - nStrat) / (nEnd - nStrat) * 100;
//继续加更新界面的代码...
};
Thread thScan = new Thread(new ThreadStart(ScanTarget));
thScan.IsBackground = true;
thScan.Start();
}
private void DoControl() { OnUiControl uicontrol = this.ScanTarget; if (this.InvokeRequired) { this.Invoke(uicontrol); } }
this.Invoke方法会使用主线程去操作的。。。。
而这是应该用后台线程去操作耗时的东东。this.Invoke(uicontrol);
这种多线程有什么意义呢?
private Action<string> _reportIP;
private void button1_Click(object sender, EventArgs e)
{
//回调处理
this._reportIP = ip => this.richTextBox1.AppendText(ip + "\r");
Thread thScan = new Thread(new ThreadStart(ScanTarget));
thScan.IsBackground = true;
thScan.Start();
}
private void ScanTarget()
{
string strIPAddress = "172.30.81."; //ip段
int nStrat = Int32.Parse("2"); //开始扫描地址
int nEnd = Int32.Parse("50"); //终止扫描地址
//开始扫描(耗时操作)
for (int i = nStrat; i <= nEnd; i++)
{
string strScanIPAdd = strIPAddress + i.ToString();
IPAddress myScanIP = IPAddress.Parse(strScanIPAdd); //转换成IP地址
try
{
//当执行下面这个操作时候比较耗时
IPHostEntry myScanHost = Dns.Resolve(strScanIPAdd);
this._reportIP(myScanHost.HostName);
}
catch (Exception error)
{
MessageBox.Show(error.Message);
}
}
}