2012年6月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) {}
        For i As Integer = 0 To 254
            LanHost(i, 0) = ""
            LanHost(i, 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, 0) = ScanIP.ToString()
            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) <> "" Then
                lv = New ListViewItem()
                lv.Text = LanHost(i, 1)
                lv.SubItems.Add(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}", LocalIPStr, 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

沒有留言:

張貼留言