温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c#中怎么实现多线程程序设计

发布时间:2021-06-16 14:51:29 来源:亿速云 阅读:153 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关c#中怎么实现多线程程序设计,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1、打开Microsoft Visual Studio 2010软件,选择新建项目,创建一个名叫ScanComputer的Windows窗体应用程序项目,(当然项目名大家可以自己任意取,这个对我们的实验没影响。)接着点击【确定】即可。

c#中怎么实现多线程程序设计

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

c#中怎么实现多线程程序设计

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;

    }

c#中怎么实现多线程程序设计

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 );

    }

  }

c#中怎么实现多线程程序设计

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));

    }

c#中怎么实现多线程程序设计

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);

      }

    }

c#中怎么实现多线程程序设计

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();

    }

  }

c#中怎么实现多线程程序设计

8、将下面代码添加到MainForm.cs,多线程应用程序就做好了

private void numericUpDownStart_ValueChanged(object sender, EventArgs e)

    {

      numericUpDown5.Value = numericUpDown1.Value;

      numericUpDown6.Value = numericUpDown2.Value;

      numericUpDown7.Value = numericUpDown3.Value;

    }

c#中怎么实现多线程程序设计

上述就是小编为大家分享的c#中怎么实现多线程程序设计了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI