Hi!
i am making a program invb.net on threading but when i execute it produce the error on the progbar.Value = newval
my program is
Imports System.Threadin g
Public Class Form1
Private trd As Thread
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
MsgBox("this is main thread")
End Sub
Private Sub threadtask()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = progbar.Step * rnd.Next(-1, 2)
newval = progbar.Value + stp
If newval > progbar.Maximum Then
newval = progbar.Maximum
ElseIf newval < progbar.Minimum Then
newval = progbar.Minimum
End If
progbar.Value = newval here is error and error is
Cross-thread operation not valid: Control 'progbar' accessed from a thread other than the thread it was created on.
Thread.Sleep(10 0)
Loop
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
trd = New Thread(AddressO f threadtask)
trd.IsBackgroun d = True
trd.Start()
End Sub
End Class
i am making a program invb.net on threading but when i execute it produce the error on the progbar.Value = newval
my program is
Imports System.Threadin g
Public Class Form1
Private trd As Thread
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
MsgBox("this is main thread")
End Sub
Private Sub threadtask()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = progbar.Step * rnd.Next(-1, 2)
newval = progbar.Value + stp
If newval > progbar.Maximum Then
newval = progbar.Maximum
ElseIf newval < progbar.Minimum Then
newval = progbar.Minimum
End If
progbar.Value = newval here is error and error is
Cross-thread operation not valid: Control 'progbar' accessed from a thread other than the thread it was created on.
Thread.Sleep(10 0)
Loop
End Sub
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
trd = New Thread(AddressO f threadtask)
trd.IsBackgroun d = True
trd.Start()
End Sub
End Class
Comment