Solved

threading: illegel cross-thread help

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

624 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