当前位置: 移动技术网 > IT编程>开发语言>c# > c#多线程程序设计实例方法

c#多线程程序设计实例方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

相信很多人都了解c#语言,但是对于c#语言编写应用程序的经验不够多,所以经常为没有实例练习而烦恼吧。今天小编给大家介绍下c#里的多线程技术。主要是让大家学会线程的创建和启动方法,理解在线程中如何通过委托和窗体控件交互,同时练习ipaddress类、dns类、iphostentry类的基本用法。

1、打开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;

    }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网