By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Threads & Properties

P: n/a
I'm writing a VB.NET app that controls some equipment. Some of the control
functions run on separate threads. For example the temperature control
object reads in the temperature value and adjusts the heater/cooler
accordingly and continuously loops on its own thread doing that. The temp.
control object has a property called SetPoint that can be set by another
thread. My question is, do I need to lock the writing of the value in the
property's set statement because the Execute routine (on its own thread)
will be reading the setpoint? and is it as simple as something like:

Public Class TempControl
Private _sp As Single

Public Property SetPoint() As Single
Get
Return _sp
End Get
Set (Value As Single)
SyncLock(_sp)
_sp = Value
End SyncLock
End Set
End Property

'Execute runs on its own thread, reading the _sp value and adjusting the
temperature output accordingly.
Private Sub Execute()
<do code here to control temperature>
End Sub
End Class
Thanks

Dave Taylor
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
No, you shouldn't need a SyncLock on classes with one simple variable.

*********

You would need a SyncLock if your class looked like this...
Public Class TempControl
Private _sp As Single
Private _timeStamp As Date
The reason you would need it in this case is that the thread may change _sp, but not have a chance to change _timeStamp before a read starts.

--
Jonathan Allen
"Dave Taylor" wrote:
I'm writing a VB.NET app that controls some equipment. Some of the control
functions run on separate threads. For example the temperature control
object reads in the temperature value and adjusts the heater/cooler
accordingly and continuously loops on its own thread doing that. The temp.
control object has a property called SetPoint that can be set by another
thread. My question is, do I need to lock the writing of the value in the
property's set statement because the Execute routine (on its own thread)
will be reading the setpoint? and is it as simple as something like:

Public Class TempControl
Private _sp As Single

Public Property SetPoint() As Single
Get
Return _sp
End Get
Set (Value As Single)
SyncLock(_sp)
_sp = Value
End SyncLock
End Set
End Property

'Execute runs on its own thread, reading the _sp value and adjusting the
temperature output accordingly.
Private Sub Execute()
<do code here to control temperature>
End Sub
End Class
Thanks

Dave Taylor

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.