dcsimg
Results 1 to 34 of 34

Thread: [RESOLVED] How to create timer without interface ?

  1. #1

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Resolved [RESOLVED] How to create timer without interface ?

    I need to replace the common Timer control with a routine that shouldn't have an interface. It would be preferable to create an event around the RC5 timing function as long as this library has some useful features for parsing files. In my initial code I set the timer interval on 10.000 ms (10sec) and all what I need is to trigger a process after each 300 seconds. I don't know how precise is that timer function embedded in the RC5 library but for my needs it is more important the simplicity than accuracy. Thank you.
    Code:
    Private Sub Timer1_Timer()
        Static t As Integer 
           t = t + 10 
    
        If t = 300 Then      '300 sec = 5 min
           'do something
           t = 0     'reset t to start another 5 minute
        End If
    End Sub
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,360

    Re: How to create timer without interface ?

    May want to wait until RC5 users reply. I'd be kinda shocked if Olaf didn't have what you want already included in RC5.

    Without RC5, we can use APIs and a bas module to create a timer that not associated with any window. You will want to look at the MSDN documentation for SetTimer API. Also, I'm sure this forum has examples of SetTimer usage.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    312

    Re: How to create timer without interface ?

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:09 AM.

  4. #4
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,607

    Re: How to create timer without interface ?

    Method1:

    Code:
    Option Explicit
    
    Private WithEvents T As cTimer
    
    Private Sub StartTimer()
        Set T = New_c.Timer(100, True)      '--- 100 millisecond
    End Sub
    
    Private Sub StopTimer()
        Set T = Nothing
    End Sub
    
    Private Sub T_Timer()
        'do something
    End Sub
    Method 2:
    Code:
    Option Explicit
    
    Private WithEvents T As cTimer
    
    Private Sub StartTimer()
        If T Is Nothing Then
            Set T = New_c.Timer
        End If
        T.Interval = 100            '--- millisecond
        T.Enabled = True
        
    End Sub
    
    Private Sub StopTimer()
        If Not T Is Nothing Then
            T.Enabled = False
            T.Interval = 0
        End If
    End Sub
    
    Private Sub T_Timer()
        'do something
    End Sub
    In addition, you could use Timer.Tag to make some other logical judgments.
    Last edited by dreammanor; Feb 6th, 2019 at 10:33 PM.

  5. #5
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,607

    Re: How to create timer without interface ?

    If you just want to test the running time of the program, you could use the following two methods:

    Method1:
    Code:
    Option Explicit
    
    Public Sub Test_01()
        New_c.Timing True
        
        'do something
        
        MsgBox New_c.Timing
        
    End Sub
    Method2:
    Code:
    Option Explicit
    
    Public Sub Test_01()
        Dim d As Double
        
        d = New_c.HPTimer
        
        'do something
        
        d = New_c.HPTimer - d
        
        MsgBox d
        'Or
        MsgBox "Timing: " & Format$(d, "#,##0.0000") & " seconds."
        
    End Sub
    Last edited by dreammanor; Feb 6th, 2019 at 09:42 PM.

  6. #6
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,607

    Re: How to create timer without interface ?

    Delete duplicated post.
    Last edited by dreammanor; Feb 6th, 2019 at 09:46 PM.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,529

    Re: How to create timer without interface ?

    If your program is sitting in a buzz-loop I doubt SetTimer is going to help much:

    When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.

  8. #8

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Re: How to create timer without interface ?

    Thank you all for suggestions. The main issue is I cannot declare the WithEvents at a bas module level. Besides there is no issue to create a timer (without window) that does measure the duration from T1 to T2 but to create a timer event considering that I have only a standard module with Public Sub Main() procedure. Until now the main trigger was inside Timer1_Timer event but in these new conditions (no form) it should somehow related to the Main procedure.
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  9. #9
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    312

    Re: How to create timer without interface ?

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:09 AM.

  10. #10

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Re: How to create timer without interface ?

    Quote Originally Posted by dilettante View Post
    If your program is sitting in a buzz-loop I doubt SetTimer is going to help much:
    Yes, you are right. This small program will be deployed as a service on server and it is known for this kind of things there are not accepted GUI apps. The purpose is to keep in monitoring a small mail inbox parsing the emails once every 5 minutes. And for sure I imagine this solution with an error handler that should to disable the timer process.
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,429

    Re: How to create timer without interface ?

    {p.s. You posted before I did. If the monitoring is something that has to be done continually, then sleeping for five minutes as mentioned here is not an option}

    If you don't have a user interface, and you don't want your program to do anything but wake up every 5 minutes, and do something, then perhaps you don't need a timer at all, per se, just have a loop, do a Sleep for the amount of time you want, wake up do your process, then loop back and Sleep again.
    Last edited by passel; Feb 7th, 2019 at 04:57 AM.

  12. #12
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    312

    Re: How to create timer without interface ?

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:09 AM.

  13. #13

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Re: How to create timer without interface ?

    Thank you Dave for your 2 alternatives. The latest solution is even simple and very ingenious . I have just a question. I compared my timer control behavior with these two solutions and I have noticed that both of them are increasing the CPU usage with 30%. Do you have any idea why the vb native timer doesn't cause such a high CPU usage ?
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  14. #14
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,607

    Re: How to create timer without interface ?

    I guess Olaf will propose a better solution. Here is my stupid way:

    Moudle1.bas
    Code:
    Option Explicit
    
    Public TimerForm As New cfTimerService
    
    Sub Main()
        
        'TimerForm.Show              '<-- Method 1
        TimerForm.StartTimer        '<-- Method 2
        
        New_c.Cairo.WidgetForms.EnterMessageLoop
        
    End Sub
    cfTimerService.cls
    Code:
    Option Explicit
    
    Public WithEvents Form As cWidgetForm
    
    Public WithEvents T As cTimer
    
    Public Sub Show(Optional Modal As FormShowConstants, Optional OwnerForm As Object, Optional ShowNonFocused As Boolean)
        Form.Visible = False
        Form.Show Modal, OwnerForm, ShowNonFocused
    End Sub
    
    Public Sub StartTimer()
        Set T = New_c.Timer(5000, True)      '--- 5000 millisecond
    End Sub
    
    Public Sub StopTimer()
        Set T = Nothing
    End Sub
    
    Private Sub Class_Initialize()
        Set Form = Cairo.WidgetForms.Create(vbSizable, "Timer Service", False, 600, 480)
        Form.Visible = False
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        StopTimer
    End Sub
    
    Private Sub T_Timer()
        StopTimer
        
        'do something
        MsgBox "Hello !"
        
        StartTimer
        
    End Sub
    The above method takes up only a small amount of CPU processing cycles.

    Also, IMO, it might be better to display a WidgetForm(TimerForm.Show) in sub Main().
    Last edited by dreammanor; Feb 7th, 2019 at 08:35 AM.

  15. #15
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    312

    Re: How to create timer without interface ?

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:08 AM.

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,448

    Re: How to create timer without interface ?

    Quote Originally Posted by Daniel Duta View Post
    Thank you Dave for your 2 alternatives. The latest solution is even simple and very ingenious . I have just a question. I compared my timer control behavior with these two solutions and I have noticed that both of them are increasing the CPU usage with 30%. Do you have any idea why the vb native timer doesn't cause such a high CPU usage ?
    Code:
        While isActive
            DoEvents
        Wend
    Code:
        Do While abort = False
            If DateDiff("s", lastTrigger, Now) > 2 Then Call TimerProc
            DoEvents
        Loop

    Those are examples of what are known as busy loops. They easy to write, east to implement, and notoriously CPU hungry. It's not that much different from putting your car into neutral and hitting the gas pedal all the way to the floor. It sends the engine into high revs... Essentially you're doing the samething with the CPU... you're sending it into this tight loop that it's executing incredibly fast. Depending on the number of cores you have, you could see any where from 25% CPU usage (quad core) to 100% CPU usage (single core) ... since you're reporting 30%, my guess is you have a quad core, and one whole core (25%) of it is being dedicated to that endless busy loop. For small quick short use loop, they're probably fine, but for something in your case, it's dangerous. There's a reason things like timers exist. There is a solution, just need to find it.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

  18. #18
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    Since you are already using vbRichClient5 and you don't want a UI, it sounds like a good time to use a Class Module with a RC5 cTimer object. Maybe something like this:

    Code:
    Option Explicit
    
    Private WithEvents mo_Timer As vbRichClient5.cTimer
    Private m_Terminating As Boolean
    
    Public Sub Disable()
       If Not mo_Timer Is Nothing Then mo_Timer.Enabled = False
    End Sub
    
    Public Sub Enable(Optional ByVal p_IntervalMs As Long = 300000)
       If p_IntervalMs < 0 Then Err.Raise 5, , "Interval must be >0ms. Passed interval: " & p_IntervalMs
       
       If mo_Timer Is Nothing Then
          Set mo_Timer = New_c.Timer(p_IntervalMs, True)
       Else
          mo_Timer.Enabled = False
          mo_Timer.Interval = p_IntervalMs
          mo_Timer.Enabled = True
       End If
    End Sub
    
    Private Sub Class_Initialize()
       If Not mo_Timer Is Nothing Then Set mo_Timer = Nothing
    End Sub
    
    Private Sub Class_Terminate()
       m_Terminating = True
       Me.Disable 
    End Sub
    
    Private Sub mo_Timer_Timer()
       mo_Timer.Enabled = False
       
       ' Perform custom processing here
       Debug.Print "In Timer Event."
       
       mo_Timer.Enabled = Not m_Terminating
    End Sub
    Then in your standard Module before you enter your service loop you could instantiate a new instance of your class and enable it. Outside your service loop, you should then disable it.

    All of the code that you need to run every 300 seconds would be placed in the Timer event in your class.

  19. #19
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,607

    Re: How to create timer without interface ?

    Hi jpbro, I tested your method and it doesn't seem to keep Sub Main() running continuously.

    Code:
    Option Explicit
    
    Private MyTimer As New Class1
    
    Sub Main()
        
        MyTimer.Enable
        
        New_c.Cairo.WidgetForms.EnterMessageLoop
        
    End Sub
    Note: OP needs to use a Timer in Sub Main() instead of Class
    Last edited by dreammanor; Feb 7th, 2019 at 09:18 AM.

  20. #20
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    Hi dreammanor,

    You are right it won't run continuously on its own, but since they mentioned that they are writing a service, I've assumed they already have a service loop in Sub Main(). Most of the examples for services have an "infinite" Do...While loop that breaks on a Stop event set by Windows when the service needs to be stopped.

  21. #21
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,607

    Re: How to create timer without interface ?

    Quote Originally Posted by jpbro View Post
    Hi dreammanor,

    You are right it won't run continuously on its own, but since they mentioned that they are writing a service, I've assumed they already have a service loop in Sub Main(). Most of the examples for services have an "infinite" Do...While loop that breaks on a Stop event set by Windows when the service needs to be stopped.
    The OP said that the CPU usage increased by 30% after using the "Do...While loop". If using my method (a hidden WidgetForm), the CPU usage will hardly increase.

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,448

    Re: How to create timer without interface ?

    Quote Originally Posted by jpbro View Post
    Hi dreammanor,

    You are right it won't run continuously on its own, but since they mentioned that they are writing a service, I've assumed they already have a service loop in Sub Main(). Most of the examples for services have an "infinite" Do...While loop that breaks on a Stop event set by Windows when the service needs to be stopped.
    If they do, then they are written poorly. Those are busy lops, which I addressed in a prior post, so I'm not going to do it again... but services should take up little to no CPU when sitting idle... not spin up 30% of hte CPU usage. So if there's a service that's using a do loop to sit there waiting, it's not a properly written service, but rather an ordinary app that's just spinning its wheels doing nothing pretending to be a service.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    A Do..While loop using MsgWaitForMultipleObjects with a short timeout shouldn't produce high CPU either. I'm not sure how the OP is establishing their program as a service - if they are using the NtSvc.ocx or something else that I don't have experience then your suggestion might be superior. If they are writing their service related code with something like this:

    http://www.smsoft.ru/en/ntservice.htm

    Then they should already have a low CPU loop running (using something like this):

    http://www.smsoft.ru/en/vbwait.htm

  24. #24

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Re: How to create timer without interface ?

    Thank you all for your hints. Meanwhile, after many searches on web, I found a beautiful solution written by a Russian years ago (Sergey Merzlikin). It seems he succeeded to avoid all inconveniences mentioned above using 2 API's in a standard module (no event, no class). I have tested it only for 10000 ms (10 sec) and the CPU usage is insignificant. Now, I intend to use it as sleep function but it is not clear to me if the time interval does matter in its behavior on long term. I could increment in a variable 10 seconds each time or I could set the function for 300 seconds from beginning. Below you have possibility to access the code.
    http://smsoft.ru/en/vbwait.htm
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

  25. #25
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    I've used Sergey's service helper TLB for many years and it has been great, so I can recommend using it too (the link again is http://www.smsoft.ru/en/ntservice.htm).

    In his example service there is a loop that looks like this:

    Code:
            Do
                ' ******************
                ' It is main service loop. Here you may place statements
                ' which perform useful functionality of this service.
                ' ******************
                ' Loop repeats every second. You may change this interval.
            Loop While MsgWaitObj(1000&, hStopPendingEvent, 1&) = WAIT_TIMEOUT
    That is your main service loop that will run will low CPU until the Windows service control manager sets the "stop pending" event. What I suggest is that if you use my timer wrapper class, you instantiate and enable it *before* the "DO" line in the service loop, and then disable and destroy it *after* the "Loop While" line. That will keep the class alive for the lifetime of the service, and you can run your timer related code in the Timer event in the timer wrapper class.

    One note - if you have long-running code in your Timer event, it can cause problems when the Window service control manager sets the stop pending event and it times-out because your app is busy. In any loops you should test to see if the stop pending event is set and abort processing so you can shutdown in a timely manner.

  26. #26
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,429

    Re: How to create timer without interface ?

    If your intention is to use it as a sleep function, why not use the sleep API call I mentioned in post #11.
    It is just a single line for the declaration and a single line for where you want to sleep.
    I tested it for 5 minutes and it worked fine.

    Code:
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    
    
       'in the code
       Sleep 300000

  27. #27
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,448

    Re: How to create timer without interface ?

    I suspect they don't have any code in regards to making it a service... given that it seems like they tried the code in posts 9 & 12 which produced the undesirable CPU usage.

    As for using MsgWaitForMultipleObjects - I'll admit it's been a looooong time sine I've needed to write a service, let alone one in VB6, so if it works great.. I've never used that call. Didn't use the NtSvc.OCX either... used some other means... I forget, it required getting down and dirty with the vbp file and messing with the application type and then registering the service with the system. I'm just leery about infinite loops being used to keep things alive like that. In the end, it may be the right thing to do, or it may not. As long as the OP understands what is going on and the pitfalls and the consequences...then at the end of the day it's really his call.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  28. #28
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    Quote Originally Posted by techgnome View Post
    I suspect they don't have any code in regards to making it a service... given that it seems like they tried the code in posts 9 & 12 which produced the undesirable CPU usage.
    I think you are right about that too - I just figured that the service plumbing would have to be "installed" eventually, so we might as well take advantage of any existing service loop that would be implemented.

    Quote Originally Posted by techgnome View Post
    As for using MsgWaitForMultipleObjects - I'll admit it's been a looooong time sine I've needed to write a service, let alone one in VB6, so if it works great.. I've never used that call. Didn't use the NtSvc.OCX either... used some other means... I forget, it required getting down and dirty with the vbp file and messing with the application type and then registering the service with the system. I'm just leery about infinite loops being used to keep things alive like that. In the end, it may be the right thing to do, or it may not. As long as the OP understands what is going on and the pitfalls and the consequences...then at the end of the day it's really his call.
    Sergey includes a non-blocking wrapper function for MsgWaitForMultipleObjects that is quite handy (and linked to in both my & Daniel's posts above). It ensures that Windows messages & inputs are still being processed by DoEvents while waiting for an event (in this case the "Stop Pending" event from the SCM). I think you'll (almost) always want an infinite loop that waits for this event with a service as that's kind of the point of a service - to run in the background forever unless explicitly stopped.

    So if you have a control program other than the Windows Services control panel that you want to use to stop your service from a GUI, you can use OpenSCManager, OpenService, ControlService, and CloseServiceHandle APIs to do this. That would require your controller app to have the appropriate permissions though. Another option would be to put a test for the existence of a named Mutex in your service loop and then exit the service when it is detected. The named mutex would be created by your custom control program to signal to your service that it is time to shutdown without needing to go through the SCM.

  29. #29
    Hyperactive Member
    Join Date
    Jun 2015
    Posts
    312

    Re: How to create timer without interface ?

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:08 AM.

  30. #30
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    Quote Originally Posted by dz32 View Post
    Or add a scheduled task to execute a normal app every x minutes
    Yes, a service is a bad choice/overkill if all the program is doing is running some code ever 300 seconds. Services should be reserved for providing always-available services to other programs. Performing a batch operation/task at regular intervals sounds much more suited to a scheduled task. Again, that's if this is the only thing the program ultimately needs to do - maybe that's just a piece of a larger puzzle where a service would be warranted.

  31. #31
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,376

    Re: How to create timer without interface ?

    To add to the pile...

    If the Service is implemented according to Sergey's example - then one does not need any timer at all...
    Just delegate to your own (cyclically running) ServiceLoop-Routine from within the Service-MainLoop:
    Code:
            Do
                ' ******************
                ' It is main service loop. Here you may place statements
                ' which perform useful functionality of this service.
                ' ******************
               Call MyServiceLoopRoutine(Now, 30) '<- run a certain Job any 30 seconds
    
                ' Loop repeats every second. You may change this interval.
            Loop While MsgWaitObj(1000&, hStopPendingEvent, 1&) = WAIT_TIMEOUT
    Within your "MyServiceLoopRoutine" (which runs every second) you can then place your own Date-based checks.
    Code:
    Private Sub MyServiceLoopRoutine(ByVal CurDate As Date, Optional ByVal JobRepeatSeconds as Long = 30)
      Static LastDate As Date
      If DateDiff("s", LastDate, CurDate) > JobRepeatSeconds  Then
         LastDate = CurDate '<- set a new static Value "to measure the Diff from" the next time
         Debug.Print "The repeating Job is now performed"
      End If
    End Sub
    HTH

    Olaf

  32. #32
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,359

    Re: How to create timer without interface ?

    @Olaf I'm sure I don't have to tell you this, but watch out for daylight savings or time zone changes! If it is critical for your periodically running code to run every X seconds, then it is better to use UTC or (my preference) the RC5 HPTimer. I typically cache the HPTimer value and test against that to make sure enough seconds have passed since the last call.

    Also, one thing I like about putting the periodically running code in a class is that the scope of work is likely to change as the project matures - especially when it seems so ill-defined up front. It can be useful to be able to add Properties to the class to help control/customize operation of what is going on in the periodic processing loop as opposed to extending the parameters accepted by the private sub (for example).

  33. #33
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,376

    Re: How to create timer without interface ?

    Quote Originally Posted by jpbro View Post
    @Olaf I'm sure I don't have to tell you this, but watch out for daylight savings or time zone changes! If it is critical for your periodically running code to run every X seconds, then it is better to use UTC or (my preference) the RC5 HPTimer. I typically cache the HPTimer value and test against that to make sure enough seconds have passed since the last call.
    Good point (with the date-value-based delta-measuring - in conjunction with potential daylight-saving switches of the System-Clock, when VBs Now is used as the TimeStamp-Value)...

    To be able to further use the comfort of VBs DateDiff- and DateAdd-functions, one could use these UTC-conversion-Helpers below (e.g. placed in a *.bas-Module):
    Code:
    Public Property Get UTCLocalOffs() As Date
      Static sDT As Object
          If sDT Is Nothing Then Set sDT = CreateObject("WbemScripting.SWbemDateTime")
             sDT.SetVarDate Now
      UTCLocalOffs = sDT.GetVarDate - sDT.GetVarDate(0)
    End Property
     
    Public Function LocaltoUTC(Optional ByVal LocalDate As Date) As Date
      If LocalDate = 0 Then LocalDate = Now
      LocaltoUTC = LocalDate - UTCLocalOffs
    End Function
    
    Public Function UTCtoLocal(ByVal UTCDate As Date) As Date
      UTCtoLocal = UTCDate + UTCLocalOffs
    End Function
    With the above helpers in place somewhere, the call-scheme of my delegation-routine in post #31 should better be changed to:
    Code:
      Call MyServiceLoopRoutine(LocaltoUTC, 30) '<- passing the result of LocaltoUTC instead of Now
    Quote Originally Posted by jpbro View Post
    Also, one thing I like about putting the periodically running code in a class is that the scope of work is likely to change as the project matures - especially when it seems so ill-defined up front. It can be useful to be able to add Properties to the class to help control/customize operation of what is going on in the periodic processing loop as opposed to extending the parameters accepted by the private sub (for example).
    Yep - in our own Service-Implementations (which are also based on Sergey's stuff), there's always a (regfree loaded, from a COM-Dll) "Service-Class" (cService),
    which the Main-ServiceLoop will then delegate into (via oMyServiceClassInstance.MyServiceLoopMethod)...

    Olaf

  34. #34

    Thread Starter
    Hyperactive Member Daniel Duta's Avatar
    Join Date
    Feb 2011
    Location
    Bucharest, Romania
    Posts
    341

    Re: How to create timer without interface ?

    Hi all,
    Meanwhile I preferred to use the code below. It was stable in time and it is maintained by an user service created with srvany.exe. (I didn't understand how the service control made by Sergey Merzlikin should be used properly). On the other hand, I avoided to use the API sleep function due to a certain effect of freezing spread around window in run time.

    Code:
    Public Sub Main()
    startPoint:
        If NewMailArrived = True Then
           Call OpenDatabase  'Open SQL Database
           Call CheckInbox    'Process mails from Inbox
           Call CloseDatabase 'Close Database
        End If
           MsgWaitObj 300000  'Wait 300 sec. = 5 min.
    GoTo startPoint           'Return to a new cycle
    End Sub
    "VB code is practically pseudocode" - Tanner Helland
    "When you do things right, people won't be sure you've done anything at all" - Reed Kimble

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