Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

threading: illegel cross-thread help

Posted on 2006-11-15
2
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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