Results 1 to 4 of 4

Thread: [RESOLVED] Problem with Thread

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Resolved [RESOLVED] Problem with Thread

    Hi everyone,

    In one application, I need use 4 simultaneous threads.

    When the threads finished, I need to update the text of a TextBox.

    So, I create 4 Textbox, and I wanna the threads change the text to FINISHED.
    Each thread change one distinct TB.

    I use this "example" code:
    Code:
        Dim Terminados As Integer = 0
    
        Private Sub frmEnviarMensagem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Start()
            Dim t1 As New Thread(AddressOf tarefa1)
            Dim t2 As New Thread(AddressOf tarefa2)
            Dim t3 As New Thread(AddressOf tarefa3)
            Dim t4 As New Thread(AddressOf tarefa4)
            t1.Start()
            t2.Start()
            t3.Start()
            t4.Start()
    
        End Sub
    
        Private Sub tarefa1()
            tbPlaca1.Enabled = True
            tbQtd1.Enabled = True
            tbPlaca1.Text = "TERMINADO..."
            Terminados = Terminados + 1
    
        End Sub
    The problem is, I have an error in the red lines.

    The error is:
    INVALIDOPERATIONEXCEPTION
    "Cross-Thread operation not valid: Control tbPlaca1 Accessed from a thread other than the thread it was created on."

    I don't use cross threading. Each TB only was accessed for the correspondent thread, and for the "main program".

    How can I do this??

    Thnak You

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Problem with Thread

    you need to invoke your controls on each new thread. here's an example:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim Terminados As Integer = 0
    4.  
    5.     Private Sub frmEnviarMensagem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'Timer1.Enabled = True
    7.         'Timer1.Start()
    8.         Dim t1 As New Threading.Thread(AddressOf tarefa1)
    9.         'Dim t2 As New Thread(AddressOf tarefa2)
    10.         'Dim t3 As New Thread(AddressOf tarefa3)
    11.         'Dim t4 As New Thread(AddressOf tarefa4)
    12.         t1.Start()
    13.         't2.Start()
    14.         't3.Start()
    15.         't4.Start()
    16.  
    17.     End Sub
    18.  
    19.     Private Sub tarefa1()
    20.         enableTextbox(tbPlaca1)
    21.         enableTextbox(tbQtd1)
    22.         SetTextBoxText("TERMINADO...")
    23.         Terminados = Terminados + 1
    24.     End Sub
    25.  
    26.     Private Delegate Sub enableTextboxInvoker(ByVal tb As TextBox)
    27.     Private Sub enableTextbox(ByVal tb As TextBox)
    28.         If tb.InvokeRequired Then
    29.             tb.Invoke(New enableTextboxInvoker(AddressOf enableTextbox), tb)
    30.         Else
    31.             tb.Enabled = True
    32.         End If
    33.     End Sub
    34.  
    35.     Private Delegate Sub SetTextBoxTextInvoker(ByVal text As String)
    36.     Private Sub SetTextBoxText(ByVal text As String)
    37.         If tbPlaca1.InvokeRequired Then
    38.             tbPlaca1.Invoke(New SetTextBoxTextInvoker(AddressOf SetTextBoxText), _
    39.                                text)
    40.         Else
    41.             tbPlaca1.Text = text
    42.         End If
    43.     End Sub
    44.  
    45. End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with Thread

    Follow the CodeBank link in my signature and check out my post on Accessing Controls From Worker Threads for a more detailed explanation of the principle Paul has used.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    285

    Re: Problem with Thread

    Dear .Paul.

    It works perfectly

    Thank you

    Post Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width