Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Timers and Threads

Timers and Threads

Scheduled Pinned Locked Moved Visual Basic
helptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rspercy65
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • R rspercy65

      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

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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

      R 1 Reply Last reply
      0
      • N N a v a n e e t h

        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

        R Offline
        R Offline
        rspercy65
        wrote on last edited by
        #3

        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 If

            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 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

        J 1 Reply Last reply
        0
        • R rspercy65

          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 If

              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 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

          J Offline
          J Offline
          Johan Hakkesteegt
          wrote on last edited by
          #4

          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.

          R 1 Reply Last reply
          0
          • J Johan Hakkesteegt

            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.

            R Offline
            R Offline
            rspercy65
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • R rspercy65

              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

              J Offline
              J Offline
              Johan Hakkesteegt
              wrote on last edited by
              #6

              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 If

                  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 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 Sub

              Move 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 = oldTemp

                  End If
              

              'Is it larger?
              If oldTemp > newTemp Then
              oldTemp -= 1
              DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp

                  End 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 If

              End Sub
              

              My advice is free, and you may get what you paid for.

              R 1 Reply Last reply
              0
              • J Johan Hakkesteegt

                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 If

                    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 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 Sub

                Move 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 = oldTemp

                    End If
                

                'Is it larger?
                If oldTemp > newTemp Then
                oldTemp -= 1
                DirectCast((Me.BaseUI1.Frame(0)), CircularFrame).ScaleCollection(0).Pointer(0).Value = oldTemp

                    End 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 If

                End Sub
                

                My advice is free, and you may get what you paid for.

                R Offline
                R Offline
                rspercy65
                wrote on last edited by
                #7

                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

                J 1 Reply Last reply
                0
                • R rspercy65

                  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

                  J Offline
                  J Offline
                  Johan Hakkesteegt
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups