Results 1 to 4 of 4

Thread: [2005] Cross-thread operation not valid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Location
    Mumbai
    Posts
    94

    Resolved [2005] Cross-thread operation not valid

    Hello friend Need help for the Following code .




    Imports System.Threading

    Public Class Form1
    'txt1 = TextBox1
    'txt2 = TextBox2
    Dim counter1 As New Counter
    Dim thread1 As New System.Threading.Thread(AddressOf counter1.count)
    Private Sub BtnStartCounting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStartCounting.Click
    txt2.Clear()
    counter1.countTo = CInt(txt1.Text)
    AddHandler counter1.FinishedCounting, AddressOf FinishedCountingEventHandler
    thread1.Start()
    End Sub

    Public Sub FinishedCountingEventHandler(ByVal Count As Integer)
    txt2.Text = Count.ToString
    End Sub
    End Class

    Public Class Counter
    Public countTo As Integer
    Public Event FinishedCounting(ByVal Numberofmarches As Integer)
    Public Sub count()
    Dim loopIndex, Total As Integer
    Total = 0
    For loopIndex = 1 To countTo
    Total += 1
    Next loopIndex
    RaiseEvent FinishedCounting(Total)
    End Sub
    End Class




    While Executing the Programm. Getting Following Error.
    -----
    Cross-thread operation not valid: Control 'txt2' accessed from a thread other than the thread it was created on.


    Help to to solve the Error.

  2. #2
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: [2005] Cross-thread operation not valid

    Use a delegate to access the control from another thread:

    Code:
    Imports System.Threading
    
    Public Class Form1
    
        Dim counter1 As New Counter
        Dim thread1 As New System.Threading.Thread(AddressOf counter1.count)
        Delegate Sub FinishedCountingCallback(ByVal Count As Integer)
    
        Private Sub BtnStartCounting_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStartCounting.Click
            txt2.Clear()
            counter1.countTo = CInt(txt1.Text)
            AddHandler counter1.FinishedCounting, AddressOf FinishedCountingEventHandler
            thread1.Start()
        End Sub
    
        Public Sub FinishedCountingEventHandler(ByVal Count As Integer)
            ' InvokeRequired required compares the thread ID of the
            ' calling thread to the thread ID of the creating thread.
            ' If these threads are different, it returns true.
            If Me.txt2.InvokeRequired Then
                Dim d As New FinishedCountingCallback(AddressOf FinishedCountingEventHandler)
                Me.Invoke(d, Count)
            Else
                txt2.Text = Count.ToString
            End If
        End Sub
    End Class
    
    Public Class Counter
        Public countTo As Integer
        Public Event FinishedCounting(ByVal Numberofmarches As Integer)
        Public Sub count()
            Dim loopIndex, Total As Integer
            Total = 0
            For loopIndex = 1 To countTo
                Total += 1
            Next loopIndex
            RaiseEvent FinishedCounting(Total)
        End Sub
    End Class

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2007
    Location
    Mumbai
    Posts
    94

    Re: [2005] Cross-thread operation not valid (solved)

    Thank You Shardox Cross-thread operation not valid is solved

  4. #4
    New Member
    Join Date
    Aug 2009
    Posts
    1

    Re: [2005] Cross-thread operation not valid

    One of the coolest and to the point post ......Thanx men

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