c#多線程程序設(shè)計(jì)實(shí)例方法
相信很多人都了解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è)計(jì)成如下圖所示。

3、雙擊【掃描】按鈕,讓它自動創(chuàng)建Click事件,然后在【掃描】按鈕的Click事件中,先判斷IP地址范圍是否符合要求,然后統(tǒng)計(jì)要掃描的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)文章
C# WPF實(shí)現(xiàn)動態(tài)3D光照效果
這篇文章主要為大家詳細(xì)介紹了如何利用C# WPF實(shí)現(xiàn)動態(tài)3D的光照效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
C#中隱藏TabControl選項(xiàng)卡標(biāo)簽的解決方案
這篇文章主要介紹了C#中隱藏TabControl選項(xiàng)卡標(biāo)簽的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析
這篇文章主要介紹了C#中32位浮點(diǎn)數(shù)Float(Real)一步步按位Bit進(jìn)行分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
舊項(xiàng)目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題
在項(xiàng)目開發(fā)過程中,不可避免的會升級開發(fā)工具。這次我在舊項(xiàng)目版本升級到新版Unity2021.2.x時,出現(xiàn)Visual?Studio無法定位等問題,這里我給大家分享下解決方法,舊項(xiàng)目升級新版Unity2021導(dǎo)致Visual?Studio無法使用的問題,需要的朋友可以參考下2021-12-12
WinForm特效之桌面上的遮罩層實(shí)現(xiàn)方法
這篇文章主要介紹了WinForm特效之桌面上的遮罩層實(shí)現(xiàn)方法,是一個非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09

