Avatar of Clay Willing
Clay Willing
Flag for United Arab Emirates asked on

UDP Data Capture Thread Issue

Hi All,

I am trying a new approach to my data capture/monitor project.  I have read up on a lot of vb.net UDP actions and methods and I have had some success.  However, I am now at a loss.  I have a server that listens on a thread for data that arrives and shows the sender ip:port and the text received.  This is derived from the code on this technotif page:
http://technotif.com/creating-simple-udp-server-client-transfer-data-using-c-vb-net/

With their example and a bit of tweaking as it was written before VS2017, I got both sender and receiver to work.  My next step was to break out of seeing both IP and text received in one box because I want to display them separately and use a traffic light if data is lost as the datastream will be 24H7.  The ultimate app goal is to handle what could be multiple UDP datagrams (up to 12 from outside my local network), all sources identified in a list box, capture the dat in hex and write to a file for 1 hour and then recapture and of course in parallel will be a window showing that all identified sources are still being received so if one is lost a label will turn from green to red.

The latest hurdle I can't seem to get over is that any of the lines after Line 18 line throws an invalidoperationexception and I can't figure out why.  All I have done is split the original concatenated text into two boxes and added a color change to the text box, which was working fine.

I have added snips of the exception and the thread menu item.

Appreciate any thoughts.

Regards,

Clay
[/code]
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim ThdUDPServer = New Thread(New ThreadStart(AddressOf ServerThread))
        ThdUDPServer.Start()
    End Sub

    Public Sub ServerThread()
        Dim UdpClient As New UdpClient(8080)
        While True
            Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
            Dim ReceiveBytes As Byte()
            ReceiveBytes = UdpClient.Receive(RemoteIpEndPoint)
            Dim ReturnData As String = Encoding.ASCII.GetString(ReceiveBytes)
            TxtDataRx.BackColor = Color.Green
            LbConnections.Items.Add(RemoteIpEndPoint.Address.ToString())
            TxtDataRx.Text = TxtDataRx.Text & vbCrLf & returnData.ToString()

        End While
    End Sub
End Class


[/code]
Formthread errorthread debug
Visual Basic.NET* udp/ip

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
Fernando Soto

Hi Clay;

According to the exception you are attempting to access a GUI control from a thread that did not create it, this is not allowed. The object ThdUDPServer is created in a new thread and the code in that thread is trying to change the control state. Please see this Microsoft web page to see how to get around this issue. How to: Make Thread-Safe Calls to Windows Forms Controls
Clay Willing

ASKER
Fernando ,
I did look into this but I can't see why the code worked at first and then adding the extra line now means the thread is broken.
As far as I can see the list box works okay in isolation but I can't change the text box too.
If I focus on the unsafe thread, do you have any pointers on what to change the code to be safe?
Fernando Soto

Hi Clay;

In the link I posted read the section called "Making Thread-Safe Calls to Windows Forms Controls" it has sample code showing what is needed.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
it_saige

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Clay Willing

ASKER
Fantastic. In that case I have a bit of work ahead of me to see how they both relate to the thread unsafe call note in msdn.
Appreciate the assistance
Next step will be to see how many instances the receiver can pick up and handle.
Also no error handling yet and no elegance exiting of the app.  Cleaning up was a big thing in charge for me but this is more a learning curve just now.
Thanks again
Clay Willing

ASKER
As per last post thanks saige
Fernando Soto

Well you can't win them all.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.