关键词搜索

源码搜索 ×
×

[VB.NET]各们,请问如何使用vb.net编写两个进程间消息通信的程序啊

发布2021-02-11浏览549次

详情内容

VB.Net程序设计:多线程处理。

最近要用到多线程程序,来处理一些数据。
在网上找了许多文章。现在会用多线程处理vb.net教程数据了,感觉程序灵活性增强了。
VB.Net-C#多线程Thread-代理委托delegate编程

搜索局域网的计算机

源代码下载地址:http://download.csdn.net/source/1159770

界面:

http://album.hi.csdn.net/app_uploads/linjimuhttps://files.jxasp.com/image/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) {}
            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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载