Results 1 to 4 of 4

Thread: Cross Thread is not Valid:

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Cross Thread is not Valid:

    Hi I am trying to call a Sub from another thread but it always throwing me an error: Cross Thread is not valid.

    here is the Sub I am calling from other thread.

    vb Code:
    1. Private Delegate Sub AppendToLogInvoker(ByVal strText As Object, ByVal color As Integer)
    vb Code:
    1. Private Sub WriteToLog(ByVal strText As Object, ByVal color As Integer) 'As Object
    2.         If Me.Log.InvokeRequired Then
    3.             Me.Log.Invoke(New AppendToLogInvoker(AddressOf WriteToLog), strText, color)
    4.         End If
    5.         Me.Log.SelectionStart = Len(Log.Text)
    6.         Me.Log.SelectionColor = System.Drawing.ColorTranslator.FromOle(color)
    7.         Me.Log.SelectedText = strText
    8.         Me.Log.SelectionStart = Len(Log.Text)
    9.         Me.Log.SelectedText = vbCrLf
    10.     End Sub

    I am calling that on other thread with this:
    vb Code:
    1. WriteToLog(" [" & System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") & "]  " _
    2.                        & "ERROR: Sending failed! " _
    3.                       , System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red))

    I am getting stock at this point


    vb Code:
    1. Me.Log.SelectionStart = Len(Log.Text)
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

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

    Re: Cross Thread is not Valid:

    The problem is that you're still accessing Me.Log on the secondary thread. When you call the WriteToLog method you have an If statement that calls Invoke if required, but then you proceed to access Me.Log OUTSIDE the If block, so that code is executed regardless of the thread. You should have an If...Else block. Follow the CodeBank link in my signature and check out my submission on Accessing Controls From Worker Threads for more information.
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Cross Thread is not Valid:

    @JM
    This is my guide when coding that..
    http://www.vbforums.com/showthread.php?t=498387

    I revise my code.
    vb Code:
    1. Private Sub WriteToLog(ByVal strText As Object, ByVal color As Integer) 'As Object
    2.         If Me.Log.InvokeRequired Then
    3.             Me.Log.Invoke(New AppendToLogInvoker(AddressOf WriteToLog), strText, color)
    4.         Else
    5.             Me.Log.SelectionStart = Len(Log.Text)
    6.             Me.Log.SelectionColor = System.Drawing.ColorTranslator.FromOle(color)
    7.             Me.Log.SelectedText = strText
    8.             Me.Log.SelectionStart = Len(Log.Text)
    9.             Me.Log.SelectedText = vbCrLf
    10.         End If
    11.     End Sub

    It works. But is that correct coding?
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

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

    Re: Cross Thread is not Valid:

    That is exactly correct. When you call the method for the first time, on the secondary thread, InvokeRequired is True so execution enters the If block and the only code executed is the Invoke call. When the method is invoked for the second time, on the UI thread, InvokeRequired is False and execution enters the Else block and the only code executed is the UI access.
    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

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