这期内容当中小编将会给大家带来有关c#中怎么实现多线程程序设计,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
成都创新互联是一家专注于成都网站设计、做网站、成都外贸网站建设公司与策划设计,湖滨网站建设哪家好?成都创新互联做网站,专注于网站建设10余年,网设计领域的专业建站公司;建站业务涵盖:湖滨等地区。湖滨做网站价格咨询:028-869222201、打开Microsoft Visual Studio 2010软件,选择新建项目,创建一个名叫ScanComputer的Windows窗体应用程序项目,(当然项目名大家可以自己任意取,这个对我们的实验没影响。)接着点击【确定】即可。

2、在【解决方案资源管理器】中,将Form1.cs改为MainForm.cs,然后从右侧工具栏中拖动控件到主窗体中,其中将Label1和Label2控件的【AutoSize】属性改为"False",【BorderStyle】属性改为“Fixed3D“,其他控件属性可以后面在设置。最后将界面设计成如下图所示。

3、双击【扫描】按钮,让它自动创建Click事件,然后在【扫描】按钮的Click事件中,先判断IP地址范围是否符合要求,然后统计要扫描的IP的个数,执行扫描操作。并在【扫描】按钮创建Click的事件中添加如下代码:
private void button1_Click(object sender, EventArgs e)
    {
      this.Cursor = Cursors.WaitCursor;
      listBox1.Items.Clear();
      string subIP = string.Format("{0}.{1}.{2}",
        numericUpDown1.Value,
        numericUpDown2.Value,
        numericUpDown3.Value);
      int start = (int)numericUpDown4.Value;
      int end = (int)numericUpDown8.Value;
      if (end < start)
      {
        MessageBox.Show("IP地址区间不正确!");
        return;
      }
      if (radioButton1.Checked)
      {
        ScanWithMultThreads(subIP, start, end);
      }
      else
      {
        Scan(subIP, start, end);
      }
      this.Cursor = Cursors.Default;
    }
4、在【解决方案资源管理器】中,找到项目名“ScanComputer”并用鼠标右键单击它,会出现一个弹出框,在弹出框中选择【添加】会出现另一个弹出框,在弹出框中选择【类】,创建一个类文件San.cs,使用多线程执行扫描操作。并添加如下代码:
class Scan
  {
    public string ip { get; set; }
    public MainForm form { get; set; }
    public void CheckComputer(Object obj) {
      string hostName = "";
      try
      {
        IPAddress ipAddress = IPAddress.Parse(ip);
        IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
        hostName = hostEntry.HostName;
      }
      catch {
        hostName = "未找到主机";
      }
      form .AddInfoDelegate(ip ,hostName );
    }
  }
5、在MainForm.cs中添加如下代码,让线程通过委托和窗体控件进行交互,同时运用了Dns类:
private delegate void GetComputerDnsDelegate(string strIP, string strHostName);
    public MainForm()
    {
      InitializeComponent();
    }
    public void AddInfoDelegate(string ip, string hostName)
    {
      GetComputerDnsDelegate d = AddInfo;
      listBox1.Invoke(d, ip, hostName);
    }
    public void AddInfo(string ip, string hostName)
    {
      listBox1.Items.Add(string.Format("IP地址:{0}\t域名:{1}", ip, hostName));
    }
6、在MainForm.cs中添加如下代码,将Scan类和主窗体联系起来。同时运用了IPAddress类和IPHostEntry类。
private void Scan(string subIP, int start, int end)
    {
      int ipCount = end - start + 1;
      for (int i = 0; i < ipCount; i++)
      {
        string ip = string.Format("{0}.{1}", subIP, start + i);
        string hostName = "";
        try
        {
          IPAddress ipAddress = IPAddress.Parse(ip);
          IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress);
          hostName = hostEntry.HostName;
        }
        catch
        {
          hostName = "未找到主机";
        }
        AddInfo(ip, hostName);
      }
    }
7、对IP地址开始时间和结束时间的定义:
private void ScanWithMultThreads(string subIP, int start, int end) {
      int ipCount = end - start + 1;
      Thread[]scanThreads=new Thread [ipCount];
      for (int i = 0; i < ipCount; i++) { 
        Scan scan=new Scan {
          ip =string .Format ("{0}.{1}",subIP ,start +i),
      form=this 
      };
      scanThreads [i]=new Thread (scan.CheckComputer);
      scanThreads [i].IsBackground=true ;
      scanThreads [i].Start();
    }
  }
8、将下面代码添加到MainForm.cs,多线程应用程序就做好了
private void numericUpDownStart_ValueChanged(object sender, EventArgs e)
    {
      numericUpDown5.Value = numericUpDown1.Value;
      numericUpDown6.Value = numericUpDown2.Value;
      numericUpDown7.Value = numericUpDown3.Value;
    }
上述就是小编为大家分享的c#中怎么实现多线程程序设计了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联成都网站设计公司行业资讯频道。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站名称:c#中怎么实现多线程程序设计-创新互联
URL链接:http://www.scyingshan.cn/article/dhjcho.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 