Results 1 to 23 of 23

Thread: Timer issue on VB6

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    8

    Re: Timer issue on VB6

    Quote Originally Posted by opus
    The code you (kusky) posted is a mixture of the code from EllisDee (datediff) and randem.
    You create the Static Seconds and update it, but you don't use it for checking!
    You use the DateDiff correctly, however you're telling the software to stop after one minute (which is something like 60 seconds AFAIK) and not 30 minutes. How did you stop the 34 seconds, exactly when the Form_lad was happening??
    did you see the messagebox at all?, since this command is after Unload.Me I don't think so (it was the other way around in Ellis code!)
    The reason my code has the seconds is simply for me to view something when im testing it.

    I have found out that no matter when i open the form it will close when the system clock hits the first full minute. The Code does not wait for 1 minute after the form was opened.

    IS the only way to make sure its full minutes to use what randem suggests and use seconds?

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Timer issue on VB6

    Sorry I haven't read all the posts but in case it may help, just place a timer in your form with interval set to e.g. 1000 (1 second) and paste this code:
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetTickCount Lib "kernel32" () As Long
    3. Dim StartTime As Long
    4. Dim EndTime As Long
    5.  
    6. Private Sub Form_Load()
    7.  
    8.     'Aplication must end after this
    9.     'time (in minutes) has elapsed
    10.     EndTime = 30
    11.     'Convert to milliseconds
    12.     EndTime = EndTime * 60 * 1000
    13.     'Elapsed milliseconds since windows was started
    14.     StartTime = GetTickCount
    15.  
    16. End Sub
    17.  
    18. Private Sub Timer1_Timer()
    19.  
    20.     'Is the elapsed time since application started
    21.     'larger than the maximum allowed time?
    22.     If GetTickCount - StartTime >= EndTime Then
    23.         Unload Me
    24.         End
    25.     End If
    26.  
    27. End Sub
    To avoid waiting, try to set EndTime to a small fraction of a minute to see how it works.
    Last edited by krtxmrtz; Jul 12th, 2007 at 12:42 AM.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Timer issue on VB6

    Quote Originally Posted by kusky
    I have found out that no matter when i open the form it will close when the system clock hits the first full minute. The Code does not wait for 1 minute after the form was opened.

    IS the only way to make sure its full minutes to use what randem suggests and use seconds?
    No, DateDiff will work fine, although if you think the user might change the system time then go with randem's counter technique.

    DateDiff() returns a 1 the moment the unit of time has changed. If you ask DateDiff() the difference in years between 12/31 and 1/1, it'll tell you a year has passed when in reality only a day has.

    What that means is that you want to use the next size down whenever the value you're looking for is less than 10. When looking for 30 minutes, checking the DateDiff() minutes differential is fine. When looking for only a single minute, however, you'll want to check for 60 seconds:

    If DateDiff("s", mdtmLastActivity, Now()) >= 60 Then

    I dislike using counters because timer controls are unreliable. If the system is busy, timer controls don't bother to fire an event. Even worse is that timers don't "save up" missed events; they just flat out skip them. If you use the counter method and set a timer's interval for 60,000 as a way of counting minutes, then you just have to hope that each minute when the timer is about to fire your program doesn't happen to be busy.

    Of course, that's the whole point of this thread -- tracking user inactivity -- but this is why I just don't like relying on timers to do any kind of calculation in general. So instead of counting intervals, I always opt for comparing the current time with the starting time.

    To illustrate timers skipping iterations, create a project, add a command button and a timer control, then paste this into the form's module:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        MsgBox "Blocker - Timer1 is not firing"
    End Sub
    
    Private Sub Form_Load()
        Timer1.Interval = 1000
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
        Static lngCount As Long
        
        lngCount = lngCount + 1
        Print lngCount & " seconds elapsed"
    End Sub
    Run it, then after a few seconds have gone by click the command button. Wait a few more seconds and then click the OK button on the messagebox.

  4. #4
    Addicted Member
    Join Date
    May 2007
    Location
    Belgium
    Posts
    156

    Re: Timer issue on VB6

    i use this code to keep track of time elapsed
    vb Code:
    1. Private Sub Timer1_Timer()
    2. Dim secs As Long
    3. Dim mins As Long
    4. Dim hour As Long
    5. secs = Val(Text1.Text + 1)
    6. Text1.Text = secs
    7. If Text1.Text = 60 Then
    8. Text1.Text = 0
    9. mins = Val(Text2.Text + 1)
    10. Text2.Text = mins
    11. End If
    12. If Text2.Text = 60 Then
    13. Text2.Text = 0
    14. hour = Val(Text3.Text + 1)
    15. Text3.Text = hour
    16. End If
    17. End Sub

    you could do a check like this:
    vb Code:
    1. if text2.text = 30 then
    2. unload me
    3. end if

    and put it on a timer
    Last edited by masterkert3; Jul 11th, 2007 at 02:03 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    8

    Resolved Re: Timer issue on VB6

    All works great now. Thanks everyone for your help. I went with Ellis suggestion and works like a charm - thanks again

  6. #6
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Timer issue on VB6

    Quote Originally Posted by kusky
    All works great now. Thanks everyone for your help. I went with Ellis suggestion and works like a charm - thanks again
    Well, the code I posted in #18 is not having the problem that EllisDee refers to in #19 as it is based on the GetTickCount function. The timer may not fire but time is still being kept track of.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Timer issue on VB6

    Quote Originally Posted by krtxmrtz
    Well, the code I posted in #18 is not having the problem that EllisDee refers to in #19 as it is based on the GetTickCount function. The timer may not fire but time is still being kept track of.
    Same basic idea as what I suggested. Using DateDiff (or the Timer function, which has the same precision as GetTickCount) has the added bonus of being native VB, so no API declarations are required.

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