Timers and Threads
-
I am using four timers and I think I need to use each one on a different thread. I am somewhat new to VB. I have learned a lot in the last 10 months. I just need someone to show me how to use threads w/timers. PLZ post some small coding on how todo this. Thanks in advance for everybodies help.
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
-
I am using four timers and I think I need to use each one on a different thread. I am somewhat new to VB. I have learned a lot in the last 10 months. I just need someone to show me how to use threads w/timers. PLZ post some small coding on how todo this. Thanks in advance for everybodies help.
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
There are two types of timers available in .NET framework 1 - System.Threading.Timer[^] 2 - System.Windows.Forms.Timer[^] The first timer runs a methods on a thread pool thread and the second one runs the method on the same thread. Second one is used with windows forms.
rspercy60 wrote:
I am using four timers and I think I need to use each one on a different thread.
What type of timer are you using? You need to tell use, why you think each one should be executed on a different thread?
rspercy60 wrote:
I just need someone to show me how to use threads w/timers. PLZ post some small
Looks at the MSDN documentation which I linked to see how timer is used. For threading, this[^] would be the excellent material available on-line. :)
Navaneeth How to use google | Ask smart questions
-
There are two types of timers available in .NET framework 1 - System.Threading.Timer[^] 2 - System.Windows.Forms.Timer[^] The first timer runs a methods on a thread pool thread and the second one runs the method on the same thread. Second one is used with windows forms.
rspercy60 wrote:
I am using four timers and I think I need to use each one on a different thread.
What type of timer are you using? You need to tell use, why you think each one should be executed on a different thread?
rspercy60 wrote:
I just need someone to show me how to use threads w/timers. PLZ post some small
Looks at the MSDN documentation which I linked to see how timer is used. For threading, this[^] would be the excellent material available on-line. :)
Navaneeth How to use google | Ask smart questions
I am using System.Windows.Forms.Timers. I have three controls, each control has to be activated at the same time. Here is one timers code.
Private Sub TMPTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMPTimer.Tick
If oldTemp < newTemp Then
oldTemp += 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End If
End IfIf oldTemp > newTemp Then oldTemp -= 1 DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp If oldTemp = newTemp Then oldTemp = newTemp DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°" TMPTimer.Enabled = False End If End If End Sub
This code is for a temperature guage, I have two other guages for wind direction and wind speed and all have to be activated at the same time. This is why I think I need threeds
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
-
I am using System.Windows.Forms.Timers. I have three controls, each control has to be activated at the same time. Here is one timers code.
Private Sub TMPTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMPTimer.Tick
If oldTemp < newTemp Then
oldTemp += 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End If
End IfIf oldTemp > newTemp Then oldTemp -= 1 DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp If oldTemp = newTemp Then oldTemp = newTemp DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°" TMPTimer.Enabled = False End If End If End Sub
This code is for a temperature guage, I have two other guages for wind direction and wind speed and all have to be activated at the same time. This is why I think I need threeds
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
I take it you need them all activated because you want the three incoming values to be taken at exact the same time? If you make all four measurements in a single timer's tick event, they will be received within less than a millisecond of each other (depending on your computer ofcourse). If you create four threads, they will each be activated from the main thread, within less than a millisecond from each other (depending on your computer ofcourse). See where I am going with this?
My advice is free, and you may get what you paid for.
-
I take it you need them all activated because you want the three incoming values to be taken at exact the same time? If you make all four measurements in a single timer's tick event, they will be received within less than a millisecond of each other (depending on your computer ofcourse). If you create four threads, they will each be activated from the main thread, within less than a millisecond from each other (depending on your computer ofcourse). See where I am going with this?
My advice is free, and you may get what you paid for.
-
THNX a lot. Worked great. I added all into 1 timer, fantastic.
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
You're welcome. By the way, the code you posted contains some flaws that will cause part of the code to never execute:
Private Sub TMPTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMPTimer.Tick
If oldTemp < newTemp Then
oldTemp += 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp'The following code will never be executed
'because you already established that oldTemp is smaller than newTemp:
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End If
End IfIf oldTemp > newTemp Then oldTemp -= 1 DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp
'The following code will never be executed
'because you already established that oldTemp is larger than newTemp:
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End If
End If
End SubMove the bit that is the same in both:
Private Sub TMPTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMPTimer.Tick
'Is it smaller?
If oldTemp < newTemp Then
oldTemp += 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTempEnd If
'Is it larger?
If oldTemp > newTemp Then
oldTemp -= 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTempEnd If
'Is it the same?
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End IfEnd Sub
My advice is free, and you may get what you paid for.
-
You're welcome. By the way, the code you posted contains some flaws that will cause part of the code to never execute:
Private Sub TMPTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMPTimer.Tick
If oldTemp < newTemp Then
oldTemp += 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp'The following code will never be executed
'because you already established that oldTemp is smaller than newTemp:
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End If
End IfIf oldTemp > newTemp Then oldTemp -= 1 DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp
'The following code will never be executed
'because you already established that oldTemp is larger than newTemp:
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End If
End If
End SubMove the bit that is the same in both:
Private Sub TMPTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TMPTimer.Tick
'Is it smaller?
If oldTemp < newTemp Then
oldTemp += 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTempEnd If
'Is it larger?
If oldTemp > newTemp Then
oldTemp -= 1
DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTempEnd If
'Is it the same?
If oldTemp = newTemp Then
oldTemp = newTemp
DirectCast(Me.BaseUI1.Frame(0).FrameCollection(0), NumericalFrame).Indicator.DisplayValue = oldTemp.ToString() & "°"
TMPTimer.Enabled = False
End IfEnd Sub
My advice is free, and you may get what you paid for.
When my program first boots up, oldTemp = 0, newTemp = 84, newTemp is the current temperature in my home town. The guage starts moving from 0 to 84. Now oldtemp = newTemp and the DirectCast statement gets executed. Now I change my zipcode to somewhere in Alaska, newTemp = 54, seeing oldTemp = 84, the guage starts moving backwards till it gets to 54 and and the DirectCast statement gets executed. My code works just as it is suppose to. So far I have not had any exceptions popup on me and I have checked it with multiple zipcodes. The temperature, wind direction and wind speed guages all move to thier correct temp, wind direction, and wind speed integers that are retrieved from yahoo.
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
-
When my program first boots up, oldTemp = 0, newTemp = 84, newTemp is the current temperature in my home town. The guage starts moving from 0 to 84. Now oldtemp = newTemp and the DirectCast statement gets executed. Now I change my zipcode to somewhere in Alaska, newTemp = 54, seeing oldTemp = 84, the guage starts moving backwards till it gets to 54 and and the DirectCast statement gets executed. My code works just as it is suppose to. So far I have not had any exceptions popup on me and I have checked it with multiple zipcodes. The temperature, wind direction and wind speed guages all move to thier correct temp, wind direction, and wind speed integers that are retrieved from yahoo.
rspercy If "You wash your feet and find a pair of socks " Then "You ARE a Redneck" End If
You know what, I read your code wrong. Sorry about that. You are right, your code will work nicely.
My advice is free, and you may get what you paid for.