Results 1 to 2 of 2

Thread: help using time

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    14

    help using time

    I am writing an application using soap messages. What I am trying to do is every 5 minutes if there is no activity send a soap command. The catch is if there is activity, for example a soap message is sent from the client, I will have to reset my current time to when the last message was sent. I am not sure how to implement this. Espicially when the current minute goes from 59 to 0 changing to the next hour. If anybody has any suggestions it would really be appreciated.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: help using time

    If you are not already using a Timer to do this, then I suggest that you do. I will assume that you have added a System.Windows.Forms.Timer object called Timer1 to your Form. There are other Timer classes, though. If you are using one of them you may need to adjust my suggestions slightly, but the principles will be the same.

    If your messages are being sent infrequently, I would think that the most desirable solution would be to reset your Timer each time it happens. To do this, you would simply call Stop and then Start on the Timer. This will start it counting with a fresh interval. This solution may not be practical if your app can have concentrated activity, though, as you would be stopping and starting the Timer repeatedly in a short space of time.

    The other option would be to use the Timer's Tick event handler like this:
    VB Code:
    1. Private Const DEFAULTINTERVAL As Integer = 5 * 60 * 1000 '5 minutes as milliseconds.
    2.  
    3.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    4.         'You must code the GetLastActivityTime function yourself.
    5.         Dim lastActivityTime As Date = Me.GetLastActivityTime()
    6.  
    7.         'Test whether the time elapsed since the last activity is greater than the time elapsed since the last Tick event.
    8.         If Date.Now.Subtract(lastActivityTime).TotalMilliseconds > Me.Timer1.Interval Then 'There has been no activity since the last Tick.
    9.             'Send message here.
    10.  
    11.             'Reset the Timer's Interval.
    12.             Me.Timer1.Interval = Me.DEFAULTINTERVAL
    13.         Else 'There has been activity since the last Tick.
    14.             'Set the Timer to Tick 5 minutes after the last activity.
    15.             Me.Timer1.Interval = Me.DEFAULTINTERVAL - Convert.ToInt32(Date.Now.Subtract(lastActivityTime).TotalMilliseconds)
    16.         End If
    17.     End Sub
    Note that this code includes a call to GetLastActivityTime. You must code this yourself as I don't know how your app does this. If you already have that value in a DateTime object then you can use that directly.

Posting Permissions

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



Click Here to Expand Forum to Full Width