這期內容當中小編將會給大家?guī)碛嘘Pc#中怎么實現多線程程序設計,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯是一家專注于成都網站設計、做網站、成都外貿網站建設公司與策劃設計,湖濱網站建設哪家好?成都創(chuàng)新互聯做網站,專注于網站建設10余年,網設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:湖濱等地區(qū)。湖濱做網站價格咨詢:028-869222201、打開Microsoft Visual Studio 2010軟件,選擇新建項目,創(chuàng)建一個名叫ScanComputer的Windows窗體應用程序項目,(當然項目名大家可以自己任意取,這個對我們的實驗沒影響。)接著點擊【確定】即可。
2、在【解決方案資源管理器】中,將Form1.cs改為MainForm.cs,然后從右側工具欄中拖動控件到主窗體中,其中將Label1和Label2控件的【AutoSize】屬性改為"False",【BorderStyle】屬性改為“Fixed3D“,其他控件屬性可以后面在設置。最后將界面設計成如下圖所示。
3、雙擊【掃描】按鈕,讓它自動創(chuàng)建Click事件,然后在【掃描】按鈕的Click事件中,先判斷IP地址范圍是否符合要求,然后統計要掃描的IP的個數,執(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、在【解決方案資源管理器】中,找到項目名“ScanComputer”并用鼠標右鍵單擊它,會出現一個彈出框,在彈出框中選擇【添加】會出現另一個彈出框,在彈出框中選擇【類】,創(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 ); } }
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#中怎么實現多線程程序設計了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯成都網站設計公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網站題目:c#中怎么實現多線程程序設計-創(chuàng)新互聯
分享地址:http://chinadenli.net/article40/dhjcho.html
成都網站建設公司_創(chuàng)新互聯,為您提供App設計、網站建設、自適應網站、定制網站、建站公司、網站導航
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯