Solved

threading: illegel cross-thread help

Posted on 2006-11-15
2
205 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now