dcsimg
Results 1 to 7 of 7

Thread: Timers and Threading

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2014
    Posts
    2

    Timers and Threading

    Hello,

    I am trying to run a timer on a different thread from the main application thread.

    I am trying to send a series of httpwebrequests at different intervals.

    for example:

    wait 5 seconds then send first requests.
    wait another 2 seconds and send the next
    wait another 2 seconds and send the next
    wait another 2 seconds and send the next

    then look repeat the cycle until the timer is stopped.



    This is my code, all ticker requests get sent at the same time and not waiting 2 seconds as I want after the first is sent, any ideas what I am doing wrong? it looks like when one thread goes to sleep another is created.

    Code:
       Dim _tickEventCounter As Integer
        Dim tmrThreadingTimer As System.Threading.Timer
        Private Delegate Sub ShowTimerEventFiredDelegate(eventTime As DateTime)
    
      Private Sub InitializeTimers()
            tmrThreadingTimer = New System.Threading.Timer(New TimerCallback(AddressOf tmrThreadingTimer_Timercallback), Nothing, System.Threading.Timeout.Infinite, 6000)
        End Sub
    
    
       Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnStartStopTicker.Click
    
            If btnStartStopticker.Text = "Start Ticker" Then
                btnStartStopTicker.Text = "Stop Ticker"
    
                'This is where i need to start timer code
                'Reset the event counter
                _tickEventCounter = 0
                tmrThreadingTimer.Change(0, 5000)
               
    
            ElseIf btnStartStopTicker.Text = "Stop Ticker" Then
                btnStartStopTicker.Text = "Start Tickerr"
    
                tmrThreadingTimer.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite)
    
            End If
    
    
        End Sub
    
    
     Private Sub tmrThreadingTimer_Timercallback(state As Object)
    
    
            Thread.Sleep(5000)
            GetTicker1()
            Thread.Sleep(2000)
            GetTicker2()
            Thread.Sleep(2000)
            GetTicker3()
            Thread.Sleep(2000)
            GetTicker4()
    
    end sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,670

    Re: Timers and Threading

    Don't use a Threading.Timer. Use a Timers.Timer and handle its Elapsed event. In the event handler, send one request. You might put all the requests into a Stack or Queue and then Pop or Dequeue the next. Alternatively, put the requests into an array or List and use an index to determine the next one, incrementing the index each time the Elapsed event is raised.

    Note that the Timers.Timer will raise Elapsed events on a secondary thread by default but, if you add one to your form in the designer, the form will be assigned to the Timer's SynchronizingObject property and events will then be raised on the UI thread. In that case, you have to explicitly set the SynchronizingObject property to Nothing. To add a Timers.Timer in the designer though, you'd have to first add it to the Toolbox because it's not there by default.

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Timers and Threading

    @ jmcilhinney
    Is there a special reason for the suggestion against the Threading.Timer.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,888

    Re: Timers and Threading

    I was going to suggest using the standard Forms.Timer in the UI thread, and launch each WebRequest as a Task when the event fires. That would be simpler, but there are several details that would be different, and the OP didn't add enough information to determine whether or not that matters. For example, launching them as Tasks from the UI thread wouldn't be dependent on whether or not the previous request worked out, which may matter. Another difference would be if the OP was doing other stuff on the background thread that shouldn't be in the UI thread.
    My usual boring signature: Nothing

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,329

    Re: Timers and Threading

    I agree using the Forms.Timer since it's not doing any thing heavy.

    OP why are you doing this can you show the http code.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,329

    Re: Timers and Threading

    Quote Originally Posted by opus View Post
    @ jmcilhinney
    Is there a special reason for the suggestion against the Threading.Timer.
    http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Timers and Threading

    Thanks for that link, read it, understood. The "not Thread-Safe" was new to me, I did expect quite the opposite!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width