欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#多線程程序設(shè)計實(shí)例方法

 更新時間:2019年02月26日 16:51:30   投稿:laozhang  
在本篇文章里小編給大家分享了關(guān)于c#多線程程序設(shè)計實(shí)例步驟,需要的朋友們學(xué)習(xí)參考下。

相信很多人都了解c#語言,但是對于c#語言編寫應(yīng)用程序的經(jīng)驗(yàn)不夠多,所以經(jīng)常為沒有實(shí)例練習(xí)而煩惱吧。今天小編給大家介紹下C#里的多線程技術(shù)。主要是讓大家學(xué)會線程的創(chuàng)建和啟動方法,理解在線程中如何通過委托和窗體控件交互,同時練習(xí)IPAddress類、Dns類、IPHostEntry類的基本用法。

1、打開Microsoft Visual Studio 2010軟件,選擇新建項(xiàng)目,創(chuàng)建一個名叫ScanComputer的Windows窗體應(yīng)用程序項(xiàng)目,(當(dāng)然項(xiàng)目名大家可以自己任意取,這個對我們的實(shí)驗(yàn)沒影響。)接著點(diǎn)擊【確定】即可。

2、在【解決方案資源管理器】中,將Form1.cs改為MainForm.cs,然后從右側(cè)工具欄中拖動控件到主窗體中,其中將Label1和Label2控件的【AutoSize】屬性改為"False",【BorderStyle】屬性改為“Fixed3D“,其他控件屬性可以后面在設(shè)置。最后將界面設(shè)計成如下圖所示。

3、雙擊【掃描】按鈕,讓它自動創(chuàng)建Click事件,然后在【掃描】按鈕的Click事件中,先判斷IP地址范圍是否符合要求,然后統(tǒng)計要掃描的IP的個數(shù),執(zhí)行掃描操作。并在【掃描】按鈕創(chuàng)建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地址區(qū)間不正確!");

        return;

      }

      if (radioButton1.Checked)

      {

        ScanWithMultThreads(subIP, start, end);

      }

      else

      {

        Scan(subIP, start, end);

      }

      this.Cursor = Cursors.Default;

    }

4、在【解決方案資源管理器】中,找到項(xiàng)目名“ScanComputer”并用鼠標(biāo)右鍵單擊它,會出現(xiàn)一個彈出框,在彈出框中選擇【添加】會出現(xiàn)另一個彈出框,在彈出框中選擇【類】,創(chuàng)建一個類文件San.cs,使用多線程執(zhí)行掃描操作。并添加如下代碼:

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 = "未找到主機(jī)";

      }

      form .AddInfoDelegate(ip ,hostName );

    }

  }

5、在MainForm.cs中添加如下代碼,讓線程通過委托和窗體控件進(jìn)行交互,同時運(yùn)用了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類和主窗體聯(lián)系起來。同時運(yùn)用了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 = "未找到主機(jī)";

        }

        AddInfo(ip, hostName);

      }

    }

7、對IP地址開始時間和結(jié)束時間的定義:

 

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,多線程應(yīng)用程序就做好了

private void numericUpDownStart_ValueChanged(object sender, EventArgs e)

    {

      numericUpDown5.Value = numericUpDown1.Value;

      numericUpDown6.Value = numericUpDown2.Value;

      numericUpDown7.Value = numericUpDown3.Value;

    }

相關(guān)文章

最新評論