VB.Net程序设计:多线程处理。
搜索局域网的计算机
源代码下载地址:http://download.csdn.net/source/1159770
界面:
http://album.hi.csdn.net/app_uploads/linjimu/20090106/162454028.png
1.使用:多线程Thread-代理委托delegate编程来处理。
代码:
Imports System.Net Imports System.Threading Public Class FrmLanPC Private LanHost As String(,) Private thread As Thread() Private LocalIPStr As String Private Delegate Sub SetTextDelegate(ByVal txt As String) Private Delegate Sub SetListViewDelegate(ByVal lv As ListViewItem) Private Event FList() Private Sub InitLanHost() LanHost = New String(254,1) {} For i As Integer = 0 To 254 LanHost(i,0) = "" LanHost(i,1) = "" Next End Sub Private Sub SetTextFunc(ByVal str As String) Me.TextBox1.Text = str End Sub Private Sub AddLvFunc(ByVal lv As ListViewItem) Me.ListView1.Items.Add(lv) End Sub 'listview1 显示搜索主机 Private Sub ListLanHost() Dim SetTxtInst As New SetTextDelegate(AddressOf SetTextFunc) Me.TextBox1.Invoke(SetTxtInst,LanHost.GetLength(0).ToString) Dim AddLvInst As New SetListViewDelegate(AddressOf AddLvFunc) Dim lv As ListViewItem For i As Integer = 0 To 254 If LanHost(i,0) <> "" Then lv = New ListViewItem() lv.Text = LanHost(i,1) lv.SubItems.Add(LanHost(i,0)) ListView1.Invoke(AddLvInst,lv) End If Next End Sub Private Sub FrmLanPC_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load InitLanHost() '开始就注册事件 'AddHandler Me.FList,AddressOf Me.listLanHost End Sub Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Me.ListView1.Items.Clear() Dim th As Thread = New Thread(AddressOf LanSearch) th.IsBackground = False th.Name = "SearchLanPC" th.Start() '用下面的语句。要等th执行完毕后才显示。不好。 'th.Join() '这样。就可以在线程运行完毕后,就更新控件数据。 If th.ThreadState = ThreadState.Stopped Then listLanHost() End If End Sub Private Sub LanSearch() thread = New Thread(254) {} Dim localhost As String = (Dns.GetHostEntry(Dns.GetHostName())).AddressList(0).ToString() '本地主机IP地址 LocalIPStr = localhost.Substring(0,localhost.LastIndexOf(".")) Dim threadMethod As ThreadStart For i As Integer = 0 To 254 '建立255个线程扫描IP threadMethod = New ThreadStart(AddressOf LanSearchThreadMethod) thread(i) = New Thread(threadMethod) thread(i).Name = i.ToString() thread(i).Start() If Not thread(i).Join(100) Then 'Thread.Join(100)不知道这处这么用对不对,感觉没什么效果一样 thread(i).Abort() End If '用到委托来更新PrgsBar ShowProgress(i) Me.TSLab1.Text = String.Format("搜索IP:{0}.{1}",LocalIPStr,i.ToString) 'Me.TSPrgsBar.Value = i Next 'using event or thread to access the serach result 'RaiseEvent FList() '注意注册事件or用线程 Dim th As Thread = New Thread(AddressOf ListLanHost) th.Start() End Sub ''' 多线程搜索方法 Private Sub LanSearchThreadMethod() Dim Currently_i As Integer = Convert.ToInt32(Threading.Thread.CurrentThread.Name.ToString) '当前进程名称 Dim ScanIP As IPAddress = IPAddress.Parse((LocalIPStr & ".") + Convert.ToString(Currently_i + 1)) '获得扫描IP地址 Dim ScanHost As IPHostEntry = Nothing ScanHost = Net.Dns.GetHostEntry(ScanIP) '获得扫描IP地址主机信息 If ScanHost IsNot Nothing Then LanHost(Currently_i,0) = ScanIP.ToString() LanHost(Currently_i,1) = ScanHost.HostName End If End Sub Delegate Sub ShowProgressDelegate(ByVal newPos As Integer) Private Sub ShowProgress(ByVal newPos As Integer) ' 判断是否在线程中访问 If Not Me.Invokerequired Then ' 不是的话直接操作控件 Me.TSPrgsBar.Value = newPos Me.progressBarSearch.Value = newPos Else ' 是的话启用delegate访问 Dim showProgressDl As New ShowProgressDelegate(AddressOf ShowProgress) ' 如使用Invoke会等到函数调用结束,而BeginInvoke不会等待直接往后走 Me.BeginInvoke(showProgressDl,New Object() {newPos}) End If End Sub End Class
2.使用:BackgroundWorker 组件来处理。
Imports System.Net Imports System.Threading Public Class FrmBgWorker Private LanHost As String(,) Private thread As Thread() Private LocalIPStr As String '初始化数组 Private Sub InitLanHost() LanHost = New String(254,1) = "" Next End Sub '查询主机 Private Sub LanSearch() thread = New Thread(254) {} Dim localhost As String = (Dns.GetHostEntry(Dns.GetHostName())).AddressList(0).ToString() '本地主机IP地址 LocalIPStr = localhost.Substring(0,localhost.LastIndexOf(".")) Dim threadMethod As ThreadStart For i As Integer = 0 To 254 '建立255个线程扫描IP threadMethod = New ThreadStart(AddressOf LanSearchThreadMethod) thread(i) = New Thread(threadMethod) thread(i).Name = i.ToString() thread(i).Start() If Not thread(i).Join(100) Then 'Thread.Join(100)不知道这处这么用对不对,感觉没什么效果一样 thread(i).Abort() End If Me.BgWorker.ReportProgress(0,i.ToString) Next 'using event or thread to access the serach result 'RaiseEvent FList() '注意注册事件or用线程 'Dim th As Thread = New Thread(AddressOf ListLanHost) 'th.Start() End Sub ''' 多线程搜索方法 Private Sub LanSearchThreadMethod() Dim Currently_i As Integer = Convert.ToInt32(Threading.Thread.CurrentThread.Name.ToString) '当前进程名称 Dim ScanIP As IPAddress = IPAddress.Parse((LocalIPStr & ".") + Convert.ToString(Currently_i + 1)) '获得扫描IP地址 Dim ScanHost As IPHostEntry = Nothing ScanHost = Net.Dns.GetHostEntry(ScanIP) '获得扫描IP地址主机信息 If ScanHost IsNot Nothing Then LanHost(Currently_i,1) = ScanHost.HostName End If End Sub '搜索后,显示到listview里面,因为是用BackGroundWorker组件。所以在 Private Sub ListLanHost() 'Dim AddLvInst As New SetListViewDelegate(AddressOf AddLvFunc) Dim lv As ListViewItem For i As Integer = 0 To 254 If LanHost(i,0)) ' ListView1.Invoke(AddLvInst,lv) Me.ListView1.Items.Add(lv) End If Next Me.TextBox1.Text = String.Format("搜索完毕:{0},共找到主机:{1}台。",LanHost.GetLength(0).ToString,Me.ListView1.Items.Count.ToString) End Sub Private Sub FrmBgWorker_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load InitLanHost() End Sub Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Me.BgWorker.RunWorkerAsync() End Sub Private Sub BgWorker_DoWork(ByVal sender As System.Object,ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BgWorker.DoWork LanSearch() End Sub Private Sub BgWorker_ProgressChanged(ByVal sender As System.Object,ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BgWorker.ProgressChanged Me.TextBox1.Text = String.Format("搜索IP:{0}.{1}",e.UserState.ToString) Me.PrgsBar.Value = CInt(e.UserState.ToString) End Sub Private Sub BgWorker_RunWorkerCompleted(ByVal sender As System.Object,ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BgWorker.RunWorkerCompleted ListLanHost() End Sub End Class