Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Ok, so i'm new to threading, I get the concept and have seen plenty of example code which I understand. My analogy at the minute is that threading is like having employees in an office. Each one goes away and does some work at the same time and can report back as and when.

Anyway, I have made a simple program to read some data from a com port. The data is then parsed and spat out into a richtextbox. Given the volume of data this may be somewhat resource intensive at times and therefore I'd like to put this method in its own thread.

I tried this as such, using similar code to the following, but keep getting told off by VS because I'm interacting with the rtb from a thread outside of what is allowed.

VB
Private Sub btn_Restart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Restart.Click
    ' restart button
    Call restart()
End Sub


VB
Private Sub restart()
            
            ' do some error checking
            ' and other stuff

            ' threading attempt
            Dim d As myDelegate = AddressOf ReceiveSerialData
            d.BeginInvoke(cmb_Com.Text, rtb_ComData, Nothing, Nothing)

            ' this is what I previously had to call the method before
            ' I wanted to use threading...
            'ReceiveSerialData(cmb_Com.Text, rtb_ComData)

    End Sub

    Delegate Sub myDelegate(ByVal comport As String, ByRef rtb As RichTextBox)

    Sub ReceiveSerialData(ByVal comport As String, ByRef rtb As RichTextBox)
        Try
            ' Receive sentences from a com port 
            ' spit the data out to the rtb
        Catch ex As TimeoutException
            rtb.Text = "Error: Com Port read timed out."
            Me.pic_Data.BackColor = Color.Red
            Me.pic_Checksum.BackColor = Color.Red
            btn_Restart.Enabled = True
        Catch ex As Exception
            rtb.Text = "something went wrong, try again."
            Me.pic_Data.BackColor = Color.Red
            Me.pic_Checksum.BackColor = Color.Red
            btn_Restart.Enabled = True
        Finally
            If com IsNot Nothing Then com.Close()
            Me.pic_Data.BackColor = Color.Red
            Me.pic_Checksum.BackColor = Color.Red
            btn_Restart.Enabled = True
        End Try
    End Sub


Is this easy to "transfer" the code to a thread? Any help / pointers much obliged.

Cheers
Posted
Updated 21-May-13 3:39am
v2

1 solution

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
bigcdh 21-May-13 9:55am    
Thanks SA, appreciate the response, there's plenty for me to read through and learn!
Sergey Alexandrovich Kryukov 21-May-13 9:57am    
That's for sure, but threading is a must for any more or less non-trivial programming.
You are welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900