CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2002
    Posts
    115

    Need a timer, what is the preferred method

    In a vb5 application ( I can port to VB6 if necessary) I need to run some code every 30 minutes, possibly more frequently. What is my best option for a timer? Obviously I don't want something that will tax system resources all day long.

    I should probably elaborate that the application should not be suspended while the timer is running.

    Appreciate your help.
    Last edited by Mothra; August 2nd, 2004 at 12:57 PM.

  2. #2
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651
    Do you need it to be at the top and bottom of the hour, or just 30 minutes apart? The following code may not work well if you need to run the code at a certain time.

    Put a Timer on your Form and set the Interval (the lower you set it, the closer to exactly 30 minutes your code will run).

    Code:
    Private Sub Timer1_Timer()
        
        Static s_dtmNextRunTime As Date
        
        'Stop Timer while executing code
        Timer1.Enabled = False
        
        If Now > s_dtmNextRunTime Then
            
            'Code to execute every 30 minutes
            
            'Reset s_dtmNextRunTime
            s_dtmNextRunTime = DateAdd("m", 30, Now)
            
        End If
        
        'Restart Timer
        Timer1.Enabled = True
        
    End Sub
    I'd rather be wakeboarding...

  3. #3
    Join Date
    Dec 2002
    Posts
    115
    Malleyo, Thanks for your reply. The time does not matter as long as intervals are 30 minutes.

    I'm not sure I'll have a form for this, but I will look into your example none the less. I'm aware of the timer control I have never had the need for it though. I always assumed it used quite a bit of resources compared to other methods (maybe a bad assumption). I have also found some information on using the API SetTimer and KillTimer. I'm just shooting for the least overhead since the timer will be running continuously. o it does not need to fire at a specific time, just 30 min. intervals.

    Probably this afternoon I'll compare all the methods that I know of and determine which will work the best...

  4. #4
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    The best will be SetTimer() API even if it force you to add a form, which in this case will only be used to received the event. The problem with the VB Timer control is that you cannot set it to big value, while you can make it work like proposed in the previous post, it would be better to use API imo.

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  5. #5
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Quote Originally Posted by JeffB
    The best will be SetTimer() API even if it force you to add a form,
    JeffB
    I may be wrong, but it seemed to me I was succesfully using the Api Timer without
    a form (used hwnd of desktop...or even 0...and got the id of timer as obtained by
    SetTimer api to KillTimer it later)
    Last edited by Cimperiali; August 4th, 2004 at 05:43 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  6. #6
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    Just looked in MSDN, it seems you can use a TimerProc function instead of a form handle, so you are right Cesare

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  7. #7
    Join Date
    Nov 2001
    Location
    Little Rock AR
    Posts
    255
    You could always set it up as a scheduled task.

    Terminate the app when you are done doing whatever. The task scheduler will start it every 30 minutes.

    LJ
    Hope is the feeling that you get that the feeling that you have will not last very long.

  8. #8
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923
    On second thought Cesare, if you don't have a form, you'll need to use a LOOP with DoEvents, and I think the preferred method would be to not use DoEvents considering it take lot of CPU usage*. For an application that only need to run each 30 minutes, it should not be always running like that imo.

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    Yes, JeffB, I would avoid a do...loop: better to have a form!
    To tell the truth, it happened to me to have a form but also a timerApi
    in bas that did not used form hwnd...do not remember exactly why...
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  10. #10
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    but, is the thing with timers and forms as targets merely that the api needs the address of a callback function.. everytime the timer pulses, the cpu runs the code found at that address? in which case, write your function, and then set the timer with:

    ... AddressOf MySubRoutine

    ?

    or is it that you must provide a target consumer of WM_TIMER messages? i cant remember.. maybe it's too late.. night all
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  11. #11
    Join Date
    Nov 2002
    Posts
    82
    Hallo Mothra,

    Look at the "CallBack Function" within the "CallBack Function.rar " from here.
    james

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

Featured