[VB.NET]使用Invoke时出现异常 [英] [VB.NET] Exception when using Invoke

查看:175
本文介绍了[VB.NET]使用Invoke时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我的编程技巧非常基础。我正在尝试使用Visual Basic Express 2008中的UDP创建客户端/服务器通信程序。

这是一个简单的接收器,用于我写的另一个应用程序发送的消息。



问题是它从1台计算机接收时工作正常。如果我有两台或更多台计算机发送,它会在Invoke行中经常抛出异常。



这是代码:



Hi all.

My programming skills are very basic. I am trying to make a client/server communication program using UDP in Visual Basic Express 2008.
It is a simple receiver for messages another app I've wrote sends.

The problem is that it works fine when it receives from 1 computer. If I have 2 or more computers sending, it throws exceptions quite regularly in the Invoke line.

Here is the code:

<pre lang="vb">

Private Sub MigraReceiver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	StartListening()

End sub

Private Sub ProcessPacket(ByVal ar As IAsyncResult)
        Dim Data As Byte()

        SyncLock UDPReadLock
            Try
                Data = ListenSocket.EndReceive(ar, New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0))
            Catch ex As Exception
                ' Handle any exceptions here
            Finally
                Try
                    UDPSyncResult = ListenSocket.BeginReceive(AddressOf ProcessPacket, UDPState)
                Catch ex As Exception
                    ' Do nothing
                End Try
            End Try
        End SyncLock

        ' Only attempt to process if we received data
        If Data.Length > 0 Then
            Dim Thread As New System.Threading.Thread(AddressOf SetText)
            Thread.Start(System.Text.Encoding.Unicode.GetString(Data))
            'SetText(System.Text.Encoding.Unicode.GetString(Data))
        End If
End Sub

Private Sub StartListening()
        Try
            ListenSocket = New System.Net.Sockets.UdpClient
            ListenSocket.Client.SetSocketOption(Net.Sockets.SocketOptionLevel.Socket, Net.Sockets.SocketOptionName.ReuseAddress, True)
            ListenSocket.Client.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Any, listenPort))
            UDPSyncResult = ListenSocket.BeginReceive(AddressOf ProcessPacket, UDPState)
        Catch ex As Exception
            'Handle an exception
        End Try
End Sub


Private Delegate Sub SetTextDelegate(ByVal [text] As String)


Private Sub SetText(ByVal text As String)




        Dim espera_ordem As Integer = 0


        If Me.TabControl1.InvokeRequired Then

            'THIS IS WHERE IT THROWS THE EXCEPTION:
            Me.TabControl1.Invoke(New SetTextDelegate(AddressOf SetText), New Object() {text})   
       

        Else


		'Does GUI updates.

	End if

End Sub







它在

Me.TabControl1.Invoke(New SetTextDelegate(AddressOf SetText),New Object(){text})中抛出异常'Array out of bounds'

线路。



我一直在尝试,测试和搜索3天没有成功:(

看来问题发生在我收到的时候在短时间内几包。



任何帮助都将不胜感激。谢谢。



问候,

Mike




It throws an exception 'Array out of bounds' in the
Me.TabControl1.Invoke(New SetTextDelegate(AddressOf SetText), New Object() {text})
line.

I've been trying, testing and searching for 3 days without success :(
It seems the problem occurs when I receive several packets in a short time.

Any help would be appreciated. Thanks.

Regards,
Mike

推荐答案

这篇关于[VB.NET]使用Invoke时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆