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
Solved

threading: illegel cross-thread help

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

861 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