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

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

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

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、在【解決方案資源管理器】中,找到項目名“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 );
}
}

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類和主窗體聯(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);
}
}

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,多線程應用程序就做好了
private void numericUpDownStart_ValueChanged(object sender, EventArgs e)
{
numericUpDown5.Value = numericUpDown1.Value;
numericUpDown6.Value = numericUpDown2.Value;
numericUpDown7.Value = numericUpDown3.Value;
}

相關(guān)文章
c#實現(xiàn)16進制和字符串之間轉(zhuǎn)換的代碼
#中十六進制字符串的轉(zhuǎn)換函數(shù)2007-05-05

