Error : Cross-thread operation not valid

bivunlim

Member
Joined
Dec 6, 2009
Messages
7
Programming Experience
1-3
I googled about this error, i found that you need a delegate to go around it, but I dont understand the idea. Plz Clarify.

Here's a Screenshot.
2i25rhs.jpg


Do I need to attach my code too?
 
Thank You for it. that error is over but another problem arose. Now hte program wont even run! the form appears and just stops there with Not Responding msg. Plz check this code.

I'm trying to run the updateInfinity() method in background for as long as the program runs. And it'll invoke a method refreshTL() that'll reset and re-initialize a ListView object lsvTweets.

VB.NET:
Imports Twitterizer.Framework
Imports System
Imports System.Threading
Public Class Form2
    Dim myOwn As New Twitter("iAmBivas", "bcltt351")
    'Dim frndTL As TwitterStatusCollection = myOwn.Status.FriendsTimeline
    'dim publicTL As TwitterStatusCollection = myOwn.Status.PublicTimeline
    Public Sub refreshTL()
        Dim frndTL As TwitterStatusCollection = myOwn.Status.FriendsTimeline
        Dim tempStat As New TwitterStatus()
        lsvTweets.Clear()
        For Each tempStat In frndTL
            lsvTweets.Items.Add(tempStat.TwitterUser.ScreenName & " : " & tempStat.Text)
        Next
    End Sub

    Private Sub updateInfinity()
        Do While True
            If lsvTweets.InvokeRequired Then
                Me.lsvTweets.BeginInvoke(New MethodInvoker(AddressOf updateInfinity))
            Else
                Me.refreshTL()
                Thread.Sleep(20000)
            End If
        Loop

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        myOwn.Status.Update(txtUpdate.Text)
        refreshTL()
        txtUpdate.Clear()
        txtUpdate.Focus()
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        refreshTL()
    End Sub

    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        End
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        refreshTL()
        txtUpdate.Focus()
        'Dim upd As New Threading.Thread(AddressOf Me.updateInfinity)
        'upd.Start()
        Me.BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub lsvTweets_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsvTweets.Click
        txtDetails.Text = ""
        txtDetails.Text = lsvTweets.FocusedItem.Text
    End Sub

    Private Sub txtDetails_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDetails.TextChanged

    End Sub

    Private Sub txtUpdate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUpdate.TextChanged
        If txtUpdate.TextLength > 140 Then
            Label1.ForeColor = Color.Red
        End If
        Label1.Text = txtUpdate.TextLength & " Chars"
    End Sub

    Private Sub btnBitLy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBitLy.Click
        btnShorten.Enabled = True
        txtBitly.Enabled = True
        btnBitLy.Enabled = False
    End Sub

    Private Sub btnShorten_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShorten.Click
        txtUpdate.Text &= " " & txtBitly.Text
        txtBitly.Text = ""
        btnShorten.Enabled = False
        txtBitly.Enabled = False
        btnBitLy.Enabled = True
    End Sub

    Private Sub lsvTweets_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvTweets.SelectedIndexChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtUpdate.Text = ""
        txtUpdate.Text &= "RT " & txtDetails.Text
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim tmp As String
        Dim tmpChar As Char
        Dim n As Integer = 0
        tmp = txtDetails.Text
        For Each tmpChar In tmp
            If tmpChar <> " " Then
                n = n + 1
            Else
                Exit For
            End If
        Next
        tmp = txtDetails.Text.Substring(0, n)
        txtUpdate.Text = "@" & tmp & " "
        txtUpdate.Focus()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Me.updateInfinity()
    End Sub
End Class
 
I'm afraid that your updateInfinity method is all wrong:
VB.NET:
    Private Sub updateInfinity()
        Do While True
            If lsvTweets.InvokeRequired Then
                Me.lsvTweets.BeginInvoke(New MethodInvoker(AddressOf updateInfinity))
            Else
                Me.refreshTL()
                Thread.Sleep(20000)
            End If
        Loop

    End Sub
When that gets called it's going to enter the Do loop. It will then enter the If block and invoke itself on the UI thread, then loop and invoke itself on the UI thread, then loop and invoke itself on the UI thread, etc., etc. Meanwhile, each of those invocations is going to to enter its own Do loop. They will then enter the Else block, perform the update and then put the UI thread to sleep for 20 seconds. So, you've got the secondary thread doing nothing but continually spawning more and more invocations and the UI thread doing all the work and sleeping for large blocks of time. That's not really the idea of multi-threading.

Your updateInfinity method is completely pointless. The loop should be in the DoWork event handler and it should contain only a call to refreshTL and a call to Sleep and the invocation should done in refreshTL.
 
Back
Top