c#中怎么實現(xiàn)多線程程序設(shè)計

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)c#中怎么實現(xiàn)多線程程序設(shè)計,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

公司主營業(yè)務(wù):做網(wǎng)站、成都做網(wǎng)站、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出米林免費做網(wǎng)站回饋大家。

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

c#中怎么實現(xiàn)多線程程序設(shè)計

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

c#中怎么實現(xiàn)多線程程序設(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;

    }

c#中怎么實現(xiàn)多線程程序設(shè)計

4、在【解決方案資源管理器】中,找到項目名“ScanComputer”并用鼠標右鍵單擊它,會出現(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 = "未找到主機";

      }

      form .AddInfoDelegate(ip ,hostName );

    }

  }

c#中怎么實現(xiàn)多線程程序設(shè)計

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#中怎么實現(xiàn)多線程程序設(shè)計

6、在MainForm.cs中添加如下代碼,將Scan類和主窗體聯(liá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 = "未找到主機";

        }

        AddInfo(ip, hostName);

      }

    }

c#中怎么實現(xiàn)多線程程序設(shè)計

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

    }

  }

c#中怎么實現(xiàn)多線程程序設(shè)計

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;

    }

c#中怎么實現(xiàn)多線程程序設(shè)計

上述就是小編為大家分享的c#中怎么實現(xiàn)多線程程序設(shè)計了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標題:c#中怎么實現(xiàn)多線程程序設(shè)計
鏈接分享:http://bm7419.com/article44/psciee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、服務(wù)器托管標簽優(yōu)化、搜索引擎優(yōu)化、虛擬主機、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計