newbie--Threading--sample code--Help (Cross-thread operation not v

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I grabbed a snippet from here:
http://www.tek-tips.com/pops/emailfaq.cfm?pid=0&fid=5929

I slightly modify:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim tCounter As New Thread(AddressOf RunCounter)
tCounter.Start()

End Sub


Private Sub RunCounter()
Dim Counter As Integer
Me.ProgressBar1.Maximum = 1000
Do
Me.Label1.Text = Counter & "/" & 1000
Me.ProgressBar1.Value = Counter
Counter += 1

Loop While Counter < 1000
MsgBox("job is done " & ProgressBar1.Maximum)

End Sub



Here's the run-tim error I get:

Cross-thread operation not valid: Control 'ProgressBar1' accessed from a
thread other than the thread it was created on.


Appreciate any advise...?

thx in advance
 
magellan said:
I grabbed a snippet from here:
http://www.tek-tips.com/pops/emailfaq.cfm?pid=0&fid=5929

I slightly modify:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim tCounter As New Thread(AddressOf RunCounter)
tCounter.Start()

End Sub


Private Sub RunCounter()
Dim Counter As Integer
Me.ProgressBar1.Maximum = 1000
Do
Me.Label1.Text = Counter & "/" & 1000
Me.ProgressBar1.Value = Counter
Counter += 1

Loop While Counter < 1000
MsgBox("job is done " & ProgressBar1.Maximum)

End Sub



Here's the run-tim error I get:

Cross-thread operation not valid: Control 'ProgressBar1' accessed from a
thread other than the thread it was created on.

It simply means the Control was created and is running on the Main/Parent
thread, the thread the application started up on. Then you spawned a child
thread, and you cannot come across threads to access the control that's
running on the Main/Parent thread.
 
Cross-thread operation not valid: Control 'ProgressBar1' accessed from a
thread other than the thread it was created on.

Usually you cannot call form control methods from another thread. To avoid
this problem, you can use the Invoke method (which makes sure that the
progress bar methods are actually called in the parent thread by using a
delegate):

Private Sub RunCounter()
Dim Counter As Integer
Do
SetProgressBarValue(Counter)
Counter += 1
Loop While Counter < 1000
MsgBox("job is done " & GetProgressBarValue())
End Sub


Public Delegate Sub SetProgressBarValueDelegate(ByVal Value As Integer)
Sub SetProgressBarValue(ByVal Value As Integer)
If Me.InvokeRequired Then
Me.Invoke(New SetProgressBarValueDelegate(AddressOf
Me.SetProgressBarValue), New Object() {Value})
Else
Me.ProgressBar1.Value = Value
End If
End Sub

Public Delegate Function GetProgressBarValueDelegate() As Integer
Function GetProgressBarValue() As Integer
If Me.InvokeRequired Then
Return CType(Me.Invoke(New GetProgressBarValueDelegate(AddressOf
Me.GetProgressBarValue)), Integer)
Else
Return Me.ProgressBar1.Value
End If
End Function

Hope this helps!

-h-
 
Back
Top