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;
using System.Net.Sockets;
using System.Collections;
using System.IO;namespace 使用线程扫描计算机
{
public delegate void UpdateList(string sIP, string sHostName); public partial class Form1 : Form
{
private System.DateTime StartTime; public Form1()
{
InitializeComponent();
Form1.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
StartTime = DateTime.Now;
string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + ".";
int Min = (int)numericUpDown4.Value;
int Max = (int)numericUpDown5.Value;
if (Min > Max)
{
MessageBox.Show("输入地址取件不合法", "错误");
return;
}
int _ThreadNum = Max - Min + 1;
Thread[] mythread = new Thread[_ThreadNum];
progressBar1.Minimum = Min;
progressBar1.Maximum = Max;
int i;
for (i = Min; i <= Max; i++)
{
int k = Max-1;
ping HostPing = new ping();
HostPing.ip = mask + i.ToString();
HostPing.ul = new UpdateList(UPdateMyList);
mythread[k] = new Thread(new ThreadStart(HostPing.scan));
mythread[k].Start();
}
} public class ping
{
public UpdateList ul;
public string ip;
public string HostName;
public void scan()
{
IPAddress myIP = IPAddress.Parse(ip);
try
{
IPHostEntry myHost = Dns.GetHostEntry(myIP);
HostName = myHost.HostName.ToString();
}
catch
{
HostName = "";
}
if (HostName == "")
HostName = "主机没有响应!";
if (ul != null)
ul(ip, HostName);
}
} void UPdateMyList(string sIP, string sHostName)
{
lock (listBox1)
{
listBox1.Items.Add(sIP + " " + sHostName); if (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++;
}
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("成功完成检测!", "提示");
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
label4.Text = ts.Seconds.ToString() + "秒";
progressBar1.Value = progressBar1.Minimum;
}
}
}
}
}
这个是代码。。mythread[k] = new Thread(new ThreadStart(HostPing.scan));
mythread[k].Start();
错误出现在这里
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;
using System.Net.Sockets;
using System.Collections;
using System.IO;namespace 使用线程扫描计算机
{
public delegate void UpdateList(string sIP, string sHostName); public partial class Form1 : Form
{
private System.DateTime StartTime; public Form1()
{
InitializeComponent();
Form1.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
StartTime = DateTime.Now;
string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + ".";
int Min = (int)numericUpDown4.Value;
int Max = (int)numericUpDown5.Value;
if (Min > Max)
{
MessageBox.Show("输入地址取件不合法", "错误");
return;
}
int _ThreadNum = Max - Min + 1;
Thread[] mythread = new Thread[_ThreadNum];
progressBar1.Minimum = Min;
progressBar1.Maximum = Max;
int i;
for (i = Min; i <= Max; i++)
{
int k = Max-1;
ping HostPing = new ping();
HostPing.ip = mask + i.ToString();
HostPing.ul = new UpdateList(UPdateMyList);
mythread[k] = new Thread(new ThreadStart(HostPing.scan));
mythread[k].Start();
}
} public class ping
{
public UpdateList ul;
public string ip;
public string HostName;
public void scan()
{
IPAddress myIP = IPAddress.Parse(ip);
try
{
IPHostEntry myHost = Dns.GetHostEntry(myIP);
HostName = myHost.HostName.ToString();
}
catch
{
HostName = "";
}
if (HostName == "")
HostName = "主机没有响应!";
if (ul != null)
ul(ip, HostName);
}
} void UPdateMyList(string sIP, string sHostName)
{
lock (listBox1)
{
listBox1.Items.Add(sIP + " " + sHostName); if (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++;
}
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("成功完成检测!", "提示");
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
label4.Text = ts.Seconds.ToString() + "秒";
progressBar1.Value = progressBar1.Minimum;
}
}
}
}
}
这个是代码。。mythread[k] = new Thread(new ThreadStart(HostPing.scan));
mythread[k].Start();
错误出现在这里
解决方案 »
- 使用C#访问Oracle,使用连接池,代码如何写,数据库端要不要做配置
- 新手问题:请问C#中WriteLine();
- C#可不可以使一个方法参数为任意类型,重载方式除外。(实在没分了,仅有的10分奉上)
- 二级域名取值问题
- List 组合排序 请给简单源代码
- 在C#中如何把Tabcontrol中的Tabpage整个隐藏掉?
- ADO.NET交流群:7917615
- 操作powerpoint怎么编译通不过啊,原文按微软网站做的啊. 请帮我啊!
- 大文本文件分析统计
- 如何实现ActiveX控件的DragDrop操作?
- 无法打开计算机“.”上的服务控制管理器。此操作可能需要其他特权。
- c#高级编程是不是这里出错了
代码自己改,多用断点,多调试,基础一定要打好
{
int k = Max-1;
这里改为 int k= i- Min;这样就能够使用