Results 1 to 17 of 17

Thread: source code for timer

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    18

    source code for timer

    hihi,
    Does anyone have timer source code in vb6??
    Or where can I find it??
    ThanK YoU.

  2. #2
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: source code for timer

    Mayby this might work for you.
    VB Code:
    1. Private Sub Form_Load()
    2.      Timer1.Interval = 1000
    3.      Label1.Caption = "100"
    4. End Sub
    5.  
    6. Private Sub Timer1_Timer()
    7.      If Label1.Caption = 0 Then
    8.           Timer1.Enabled = False
    9.           MsgBox ("100 secs is up")
    10.      Else
    11.           Label1.Caption = Label1.Caption - 1
    12.      End If
    13. End Sub
    you can change the Timer1.Interval to make it count faster or slower
    and the Label1.Caption you can change for however many seconds you want. Where the MsgBox is put what code you want to happen when that time limit is up.

  3. #3
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    Is there a way to use the same logic built in LABEL1.CAPTION but not to be displayed, I mean a timer running in the background?

    Thanks

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: source code for timer

    Off course it is. Timer can do anything, even nothing For example:

    VB Code:
    1. int i;
    2. Private Sub Timer1_Timer()
    3.     i=i+1
    4. End Sub
    Everytime Timer1 triggers (depends of an interval), i will increase for 1. Is this what you've asked?

  5. #5
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    in the Label1.Caption = "100" example in this post, its a timer that will be displayed, so its a static number, can i create a static number lets say 10 hours that will start the count down till its 0 then "Do Something". but all this in the background nothing need to be displayed.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    or count till 10 doesn't have to be a count down. as long as it cover the number of hours that I am looking for

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: source code for timer

    When the timer fires (after the .Interval amount of time [im ms]), Private Sub Timer1_Timer() runs. Do whatever you want in that sub - decrement a number, display something - it's up to you. Displaying something isn't required if you don't want a display.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  8. #8
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    I am working on a tool that have two fuctions that counts on Timer, the first one after 60 minutes will call NotePad, but this will be triggered when the user click on the Button, and the second function is a timer that will call another EXE after 10 hours without the interaction of the user.

    What am I doing wrong here:

    VB Code:
    1. Private Sub Form_Load()
    2.     Timer1.Interval = 100   'fire timer
    3.     Timer1.Enabled = True
    4.     Timer1.Interval = 60000 '1 minute
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. Static iCounter As Long
    9. Static countdown As Long
    10.  
    11.  
    12. If iCounter = 1 Then
    13.         SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    14.                Me.WindowState = vbNormal
    15.         Shell "notepad"
    16.             lCounter = 0
    17. End If
    18.     iCounter = iCounter + 1
    19.        
    20.        
    21. If countdown = 2 Then
    22.      Timer1.Enabled = False
    23.      Shell "calc"
    24. Else
    25.      countdown = countdown + 1
    26. End If
    27.  
    28. End sub

    right now evertime the first function that is the icounter starts, it calls the second one "Countdown", the countdown works well by itself when the tool stay running for the whatever the time its ment to start after its time is up, but when I call the first function that is iCounter which will call another EXE after 60 minutes it starts and it call the second CALC (Countdown) that its not supposed to. What am I doing wrong here?

    thanks

  9. #9
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    Note: I want the CALC to start only after 10 hours

  10. #10
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: source code for timer

    in timer...

    VB Code:
    1. If countdown=600 Then  '600 minutes equals 10 hours
    2.     'execution
    3. Else
    4.     countdown=countdown+1
    5. End Sub

  11. #11
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    Still have the same problem. and now the COUNTDOWN after one minute it opens hundreds of CALC. there I need to know how not to relate the two functions from each other, when the first function thats is the iCounter starts after the user click on the BUTTON it fires NOTEPAD and it calls COUNTDOWN calc. it shouldn't do that. those two are not related it should not call the second function.

    VB Code:
    1. Private Sub Form_Load()
    2.     Timer1.Interval = 100   'fire timer
    3.     Timer1.Enabled = True
    4.     Timer1.Interval = 60000 '1 minute
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. Static iCounter As Long
    9. Static countdown As Long
    10.  
    11. If iCounter = 60 Then
    12.         SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    13.                Me.WindowState = vbNormal
    14.         Shell "notepad"
    15.             lCounter = 0
    16. End If
    17.     iCounter = iCounter + 1
    18.        
    19. If countdown = 600 Then '600 minutes equals 10 hours
    20.     Shell "calc"
    21.    
    22. Else
    23.     countdown = countdown + 1
    24. End If
    25.  
    26.        
    27. End Sub

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: source code for timer

    You've given a perfect example of why you should always use OPTION EXPLICIT.

    VB Code:
    1. Private Sub Form_Load()
    2.     Timer1.Interval = 100   'fire timer
    3.     Timer1.Enabled = True
    4.     Timer1.Interval = 60000 '1 minute
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. Static iCounter As Long
    9. Static countdown As Long
    10.  
    11. If iCounter = 60 Then
    12.         SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    13.                Me.WindowState = vbNormal
    14.         Shell "notepad"
    15.             lCounter = 0 ' [HL="#FFFF80"]Should be iCounter[/HL]
    16. End If
    17.     iCounter = iCounter + 1
    18.        
    19. If countdown = 600 Then '600 minutes equals 10 hours
    20.     Shell "calc"
    21.    
    22. Else
    23.     countdown = countdown + 1
    24. End If
    25.  
    26.        
    27. End Sub

  13. #13
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: source code for timer

    Two things I have to note.

    Number one being, LOL. I've never gotten C and VB confused like so
    Quote Originally Posted by gavio
    Off course it is. Timer can do anything, even nothing For example:

    VB Code:
    1. [b]int i;[/b]
    2. Private Sub Timer1_Timer()
    3.     i=i+1
    4. End Sub
    Everytime Timer1 triggers (depends of an interval), i will increase for 1. Is this what you've asked?
    Number two being. Make sure you post proper working code. Marty seems to read through every line of every post here

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  14. #14
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    OK ,here is what I have now,

    VB Code:
    1. Private Sub Form_Load()
    2.     ' Timer1.Interval = 100   'fire timer
    3.     Timer1.Enabled = True
    4.     Timer1.Interval = 60000 '1 minute
    5. End Sub
    6.  
    7. Private Sub Timer1_Timer()
    8. Static iCounter As Long
    9. Static countdown As Long
    10.  
    11. If iCounter = 1000 Then
    12.         SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    13.                Me.WindowState = vbNormal
    14.                     iCounter = 0
    15. End If
    16.     iCounter = iCounter + 1
    17.        
    18. If countdown = 2000 Then '600 minutes equals 10 hours
    19.     Shell "calc"
    20.    
    21. Else
    22.     countdown = countdown + 1
    23. End If
    24.  
    25.        
    26. End Sub

    I used the timers as 1000 and 2000 so that I can shorter time to test. Now when the 1000 start its OK, when the 2000 timer kicks in, the I get tons of calc opening on my desktop. is there a way to make it open only one CALC, it seems its going into loops with now ending.

  15. #15

  16. #16
    Hyperactive Member
    Join Date
    Jul 2006
    Posts
    507

    Re: source code for timer

    that will do it, and it will stop the iCounter too. Thanks I think my issue is fixed, thanks again

  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: source code for timer

    Now that we've helped you, you can help us by pulling down the Thread Tools menu and clicking the Mark Thread Resolved button which will let everyone know that you have your answer.

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