Solved

threading: illegel cross-thread help

Posted on 2006-11-15
2
209 Views
Last Modified: 2010-04-23
I am writing an application which is polling ports picking up information from sensors, but my threading knowledge is not the greatest.

CLASSES / FORMS used:

nav.vb - form with navigation controls, status bar and the 'updateStatus()' items (explained a little lower)
addUnit.vb --this is the form where the thread starts
csTCPListener.vb - this is the class where most of the work goes, but during the threaded procedure, calls are made back to addUnit...

The sub below is the one called by the thread and it is the one used to poll units. When one is found it does a little work then goes to the addUnit form via the 'clientConnected' method. - which basically checks whether that unit is already connected. and does some work. THE POINT WHERE THINGS ARE GOING WRONG, i think, are when i am trying to 'updateStatus()' on the addUnit form.  This is basically just a sub that raises an event on the nav.vb form.  The handler for this simply sets the text of the status control to whatever is passed in the updateStatus parameters. -- the code for this is below the csTCPListener code...

Anyway, i know this problem is something to do with changing the value of the text from the thread, but i dont know how to solve this problem. any help would be much appreciated. Thanks, Phil

##################################
csTCPListener code:
##################################


 'Scans for any new clients
    Private Sub scanClients()

        Try

            'Syncronises any thread
            SyncLock Me

                'Creates a new TCP listener class with the set port
                Dim tcpListener As New TcpListener(portVal)
                'Starts the listener
                tcpListener.Start()

                'Sets the listening boolean to true
                boolListening = True

                'Raises an event which indicates that client searching is now enabled
                RaiseEvent Scanning()

                'Start a loop
                Do

                    'Check if the there is a connection pending and listening has not be cancelled
                    If (tcpListener.Pending = True And boolListening = True) Then

                        'Create a new socket from an accepted connection
                        Dim sktSocket As Socket = tcpListener.AcceptSocket
                        'Creates an IP end point from the end point from the sktSocket
                        Dim ipeConnectedIP As IPEndPoint = CType(sktSocket.RemoteEndPoint, IPEndPoint)

                        'Call the client connect sub procedure with the paramater of the IPEndPoint
                        clientConnected(ipeConnectedIP)

                        'Close the socket
                        sktSocket.Close()

                        'if a connection is found, stop the loop
                        'boolListening = False

                    End If

                    'Put the thread to sleep for 400 milliseconds
                    Thread.Sleep(400)

                    'Repeat the loop while listening has not been cancelled
                Loop While boolListening

                'Stop the listener
                tcpListener.Stop()

                'Raise an event which indicates that searching for new clients has stopped
                RaiseEvent EndScanning()

            End SyncLock

        Catch ex As SocketException 'Catch any exceptions from the socket

            Dim objErrorHandling = New csErrorHandling.SocketError
            CType(objErrorHandling, csErrorHandling.SocketError).Set_Error(ex.Source, ex.Message)

        Catch ex As Exception 'Catch any non socket errors

            Dim objErrorHandling = New csErrorHandling
            objErrorHandling.Set_Error(ex.Message)
   
        End Try

    End Sub



##################################
nav.vb code:  (the event / handler for changing the status text)
##################################

    'declare the event
    Public Event changeStatusText(ByVal strStatus As String)

    'raises the change status event
    Public Sub updateStatus(ByVal strStatus As String)
        RaiseEvent changeStatusText(strStatus)
    End Sub

    'handles the change of status
    Private Sub objCWE_ChangeStatusText(ByVal strStatus As String) Handles Me.changeStatusText
        'find out how to change and display the change
        stMessages.Text = strStatus
    End Sub
0
Comment
Question by:Artform04
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 17946225
Public Delegate Sub changeStatusDelegate(ByVal strStatus As String)

    Private Sub objCWE_ChangeStatusText(ByVal strStatus As String) Handles Me.changeStatusText
        If Me.InvokeRequired = True Then
            Me.BeginInvoke(New changeStatusDelegate(AddressOf Me.objCWE_ChangeStatusText), _
                New Object() {strStatus})
        Else
            stMessages.Text = strStatus
        End If
    End Sub
0
 
LVL 1

Author Comment

by:Artform04
ID: 17946252
Thanks for the help alex, that worked great.

Phil
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question