473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Variables and Threads

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?
Nov 20 '05 #1
3 1216
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?

Nov 20 '05 #2
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?


Nov 20 '05 #3
issue resolved thank you anyway........

"scorpion53061" <sc****************************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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?



Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
13
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
2
by: Oenone | last post by:
I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET web application. In the VB6 code there is a module-level object which stores the context about what the user is doing...
9
by: Randy | last post by:
Hello, I'm having a strange problem. I've got a .NET web app which uses Session variables. Sometime, not all the time, they get cross threaded...that is...one user will have another user's Session...
3
by: panic | last post by:
hello, In my web app when some button is clicked, a new thread is created to import data and that thread creates some threads too, inside those threads some session variables are updated, but...
9
by: Dave Stallard | last post by:
Pardon if this is the wrong newsgroup for this question, and/or if this question is naive. I have a multi-threaded Windows application in which certain variables/object fields are shared: one...
14
by: Rick | last post by:
We are in the process of testing a large web project that I converted from VS 2003 to VS 2005. Everything seems to be working except for a few minor things. But the main issue I have is this, I...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.