Original Post
Hi folks, Just been getting back into VB a bit for RAD (I used VB6, but it was a few years ago now). I'm using MSVS 2005 Professional (downloaded off MSDNAA). I found an example on MS Help and Support about implementing threads (find it here), and my code now looks like this: I can't see any obvious difference between this and the MS original. Problem is, when I run it, it flags up
Imports System.Threading
Public Class Form1
Private trd As Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
trd = New Thread(AddressOf ThreadTask)
trd.IsBackground = True
trd.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("This is the main thread")
End Sub
Private Sub ThreadTask()
Dim stp As Integer
Dim newval As Integer
Dim rnd As New Random()
Do
stp = ProgressBar1.Step * rnd.Next(-1, 2)
newval = ProgressBar1.Value + stp
If newval > ProgressBar1.Maximum Then
newval = ProgressBar1.Maximum
ElseIf newval < ProgressBar1.Minimum Then
newval = ProgressBar1.Minimum
End If
ProgressBar1.Value = newval
Thread.Sleep(100)
Loop
End Sub
End Class
ProgressBar1.Value = newval and throws an InvalidOperationException, with the reason "Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on." I've got no idea what the hell that's all about, this code is copied straight from MS. Is there some configuration option I'm missing here? Thanks in advance, ukdeveloper.