Hi Jan et all,
Checked out the sample like you said and obviously I am still not doing
something right though I think I am on the right track.
Please look over what I have and tell me what I am doing wrong....The new
variable though in the message box returns with the updated value when used
in the main thread still exists as null.
Dim t As New Thread(New ThreadStart(AddressOf DoTheTask))
Private Sub fmchoices_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'starting message center
TestThread()
System.Threading.Thread.CurrentThread.ApartmentState =
System.Threading.ApartmentState.STA
'not sure about this line
End Sub
Public Sub TestThread()
Dim t As New Thread(New ThreadStart(AddressOf DoTheTask))
t.IsBackground = True
t.Start()
End Sub
Public Sub DoTheTask()
Dim j As Integer
Dim amotherinteger As Integer
Dim row As DataRow
Do
Try
sql2 = "SELECT comment, GETDATE()as timeofentry FROM
COMMENTS"
SqlDataAdapter1 = New SqlClient.SqlDataAdapter(sql2,
SqlConnection1)
Dim dv1 As DataView
SqlDataAdapter1.Fill(Dscomments1.Tables(0))
Dscomments1.AcceptChanges()
SqlConnection1.Close()
timeofentry =
Dscomments1.Tables(0).Rows(0).Item("timeofentry")
Dscomments1.Tables(0).Columns.Remove("timeofentry")
Dscomments1.AcceptChanges()
UpdateProgress()
Catch ex As Exception
End Try
Dim i As Integer
Dim r As String
Try
For i = 0 To Dscomments1.comments.Rows.Count - 1
For Each row In Dscomments1.comments.Rows
CommentsList.Add(row("comment"))
Next
Next
Dim icount As Integer
icount = 0
For i = 0 To CommentsList.Count - 1
Label8.Text = CommentsList.Item(i)
t.Sleep(10000)
Label8.Refresh()
icount = icount + 1
If icount = 2 Then
Label8.Text = "Updating Message Center....."
Label8.Refresh()
t.Sleep(2000)
End If
Next
Catch ex As Exception
End Try
Loop
End Sub
'This function is executed on a background thread - it marshalls calls to
'update the UI back to the foreground thread
Public Sub ThreadProc()
Try
Dim mi As MethodInvoker = New MethodInvoker(AddressOf
Me.TestThread)
While True
'Call BeginInvoke on the Form
Me.BeginInvoke(mi)
Thread.Sleep(500)
timeofentry2 = timeofentry
MsgBox(timeofentry2)
End While
Catch ex As System.Threading.ThreadInterruptedException
'Thrown when the thread is interupted by the main thread -
exiting the loop
'Simply exit...
MsgBox(ex.Message)
Catch
'All other exceptions - Do nothing...
End Try
End Sub
'This function is called from the background thread
Private Sub UpdateProgress()
timeofentry2 = timeofentry
MsgBox(timeofentry2 & " Update Progress")
End Sub
'Stop the background thread
Private Sub StopThread()
If Not (t Is Nothing) Then
t.Interrupt()
t = Nothing
End If
End Sub
http://samples.gotdotnet.com/quickstart/howto/doc/WinForms/WinFormsThreadMar able