|
-
May 20th, 2007, 11:55 PM
#1
Thread Starter
Lively Member
[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.
-
May 21st, 2007, 01:22 AM
#2
Lively Member
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
-
May 21st, 2007, 11:07 PM
#3
Thread Starter
Lively Member
Re: [2005] Cross-thread operation not valid (solved)
Thank You Shardox Cross-thread operation not valid is solved
-
Jan 8th, 2010, 11:06 AM
#4
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|