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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. passing parameter in system.threading.timer

passing parameter in system.threading.timer

Scheduled Pinned Locked Moved Visual Basic
questiondata-structures
7 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.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    I have an application in which i want to run multiple threads periodically.For this i am using the system.threading.timer in an array. When i start the timer i also want to pass a string as a parameter to the timer callback method.There is a parameter; 'state as object'.Can i pass the the string through this?

    For j As Integer = 0 To m_TimerList.Count - 1
    'Dim oCallback As New TimerCallback(AddressOf timer1_tick)
    Dim s As String = "1"
    m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), New Object() {s}, 1, 5000)
    k = k + 1
    Next

    How do i get the string in the timer callback method 'timer1_tick'

    A 1 Reply Last reply
    0
    • S sohaib_a

      I have an application in which i want to run multiple threads periodically.For this i am using the system.threading.timer in an array. When i start the timer i also want to pass a string as a parameter to the timer callback method.There is a parameter; 'state as object'.Can i pass the the string through this?

      For j As Integer = 0 To m_TimerList.Count - 1
      'Dim oCallback As New TimerCallback(AddressOf timer1_tick)
      Dim s As String = "1"
      m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), New Object() {s}, 1, 5000)
      k = k + 1
      Next

      How do i get the string in the timer callback method 'timer1_tick'

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      Use casting as per the example on MSDN http://msdn.microsoft.com/en-us/library/system.threading.timercallback.aspx[^] Alan.

      S 1 Reply Last reply
      0
      • A Alan N

        Use casting as per the example on MSDN http://msdn.microsoft.com/en-us/library/system.threading.timercallback.aspx[^] Alan.

        S Offline
        S Offline
        sohaib_a
        wrote on last edited by
        #3

        I am still not getting it...dont know how to adapt the example for my case. Private Sub timer1_tick(ByVal state As Object) Dim j As String = DirectCast(state, String) End Sub this gives an error.

        A 1 Reply Last reply
        0
        • S sohaib_a

          I am still not getting it...dont know how to adapt the example for my case. Private Sub timer1_tick(ByVal state As Object) Dim j As String = DirectCast(state, String) End Sub this gives an error.

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          Ok, what error, an invalid cast exception maybe? I don't programme in VB but I think the answer may be to pass s directly not as New Object() {s}.

          m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), s, 1, 5000)

          Alan.

          S 1 Reply Last reply
          0
          • A Alan N

            Ok, what error, an invalid cast exception maybe? I don't programme in VB but I think the answer may be to pass s directly not as New Object() {s}.

            m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), s, 1, 5000)

            Alan.

            S Offline
            S Offline
            sohaib_a
            wrote on last edited by
            #5

            thanks alot.that worked, Dim s as string m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), s, 1, 5000) in the timer callback method... Private Sub timer1_tick(ByVal state As Object) Dim st As String = state End Sub NO casting...what is it used for anyway?

            G A 2 Replies Last reply
            0
            • S sohaib_a

              thanks alot.that worked, Dim s as string m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), s, 1, 5000) in the timer callback method... Private Sub timer1_tick(ByVal state As Object) Dim st As String = state End Sub NO casting...what is it used for anyway?

              G Offline
              G Offline
              Gideon Engelberth
              wrote on last edited by
              #6

              It is used for when you have Option Strict on (which you should turn on by default in the Options menu)

              1 Reply Last reply
              0
              • S sohaib_a

                thanks alot.that worked, Dim s as string m_TimerList(j) = New System.Threading.Timer(m_Timercallbacklist(j), s, 1, 5000) in the timer callback method... Private Sub timer1_tick(ByVal state As Object) Dim st As String = state End Sub NO casting...what is it used for anyway?

                A Offline
                A Offline
                Alan N
                wrote on last edited by
                #7

                Glad the problem is sorted now. The compiler has done an implicit cast of Object to String but if you were to specify "Option Strict On" in your code you would have had to explicitly code the cast. It's not a bad idea to code with strict on as it forces you to state your intentions more precisely, i.e. this parameter of type Object is actually a String and must not be anything else. The explicit cast does that for you. Many implicit conversions will succeed and then you can be left with a major problem debugging difficult to comprehend logic errors in your code. Option Strict On will stop many of these errors before they happen and make your code more reliable. Alan.

                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