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.ApartmentSta te =
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
"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
Check out this example @ GotDotNet:
http://samples.gotdotnet.com/quickst...FormsThreadMar shalling.aspx
Windows Forms controls can only execute on the thread on which they were
created, that is, they are not thread-safe. If you want to get or set
properties, or call methods, on a control from a background thread, the
call must be marshaled to the thread that created the control.
There are five functions on a control that are safe to call from any
thread: InvokeRequired, Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all
other method calls, you should use one of the invoke methods.
By default, Windows marshals the calls for you. However, if you are making
multiple calls to a control, it is much more efficient to create a method
that executes those calls and make the one cross-thread call yourself. You
make the cross-thread call by calling one of the Control.Invoke methods.
The Invoke methods take a reference to a delegate. Typically, this delegate is
an instance of the MethodInvoker delegate.
--
Greetz
Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
"scorpion53061" <sc****************************@yahoo.com> wrote in
message news:uW**************@TK2MSFTNGP11.phx.gbl... In a seperate thread I assign a variable called timeofentry based on the
output of the GetDate function on my SQL Server. The idea is to be able
to base the time of the users actions outside of the time on their
computer. A message box confirms that the time was recorded All seems well.
In the main thread when attempting to record this variable's activity it
seems stuck on the inital value this variable contained, though it
indicates the variable changed in the other thread.
Any ideas to make the threads work together on this?