Results 1 to 13 of 13

Thread: [RESOLVED] How to run periodically a process

  1. #1

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

    Resolved [RESOLVED] How to run periodically a process

    Hello,
    I want to check automatically, at certain time intervals, if a folder has some files which I am interested in. For this purpose I chose to use the classic timer control checking per second if my time event ( my preset time = timer) is met. However, I feel it is not the best approach and I would appreciate any other opinions about it.
    Code:
    Private Sub Form_Load()
        Timer1.Interval = 100
    End Sub
    
    Private Sub Timer1_Timer()
         Dim clock As String
             clock = Left$(Format(Time, "hh:mm:ss AM"), 8)
      
            Label1.Caption = clock
         If clock = "14:25:00" Or clock = "16:10:00" Or clock = "17:30:00" Then
            Call checkFolder
         End If
    End Sub
    
    Private Sub checkFolder()
           'check if some files exist in a folder
    End Sub
    Last edited by Daniel Duta; Nov 16th, 2013 at 06:55 AM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to run periodically a process

    Well there are other options, the windows scheduler may be an option depending on what you are doing.

    In any case checking once a second and then checking for = down to the second can fail as there is no way to be 100% sure that the timer will fire at exactly one second. If something else is going on in the program then the timer could be delayed a bit and the next second could tick over in which case the program would not execute the code until the next listed time.

    Rather than using the 3 = or statements I would use a var for the next time to process and then check to see if the current time > scheduled time and execute if true then increment the next scheduled time

  3. #3

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

    Re: How to run periodically a process

    Thanks for your quick response. Well, I would like to process automatically (without any manual checking) some xls files from a certain location whenever the folder that contain them is filled. The time interval is stable (3 hours) but I would prefer to be able to check the folder from time to time (let say 15 minute) . You suggest a "windows scheduler" but honestly I cannot imagine a scheduler without a timer (control, API function or whatever) and that timer to count not by second but by 15 minute . I agree that your idea based on a incremented scheduler is a better approach.

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to run periodically a process

    Do you care what 'time' it is when you make your checks of the folder, or is just checking EVERY 15 minutes okay? Do you want to do this 24/7, or just during certain hours of certain days?

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to run periodically a process

    Another option would be to use VB.Net and the FileSystemWatcher which will fire an event when the file is created/changed

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to run periodically a process

    For example, if you wanted to check every 15 minutes between 1300 and 1700, something like this:
    Code:
    Private Sub Form_Load()
        Timer1.Interval = 900000 '15 minutes
    End Sub
    
    Private Sub Timer1_Timer()
         If Hour(Now) > 12 And Hour(Now) < 18 Then
            checkFolder
         End If
    End Sub
    
    Private Sub checkFolder()
           'check if some files exist in a folder
    End Sub

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to run periodically a process

    Timer1.Interval = 900000
    Timer value can not be set that high, you would need to use a smaller interval and a counter

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to run periodically a process

    But, that could be improved easily as well, and setting the timer to true at 1300, and to false at 1800 (and modifying the timer event slightly)....that way the timer is not being run 24/7, just the program.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to run periodically a process

    Didnt know that....thx DM. Yeah, smaller interval and a counter would work just fine.

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to run periodically a process

    Is this better DM?
    Code:
    Private Sub Form_Load()
        Timer1.Interval = 60000 '60 seconds
    End Sub
    
    Private Sub Timer1_Timer()
        Dim tInterval As Integer
        tInterval = tInterval + 30  'every minute tInterval increases by 30
        If tInterval > 450 Then  'after 15 minutes tInterval will equal 450
            If Hour(Now) > 12 And Hour(Now) < 18 Then
                checkFolder
            End If
            tInterval = 0 'reset the variable to start another 15 minute cycle
        End If
    End Sub
    
    Private Sub checkFolder()
           'check if some files exist in a folder
    End Sub

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to run periodically a process

    Yes but, need to change the Dim in the timer to Static else it will not retain its value

    Also curious why you increment the value by 30 and check for >450

    Would it not be more straight forward to increment by 1 and check for >15 ?

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: How to run periodically a process

    True...
    I don't know....just grabbed a number outa the air, and divided: ~smile~ At my age, 30 seems like 1!!!!

  13. #13

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

    Re: How to run periodically a process

    Hello guys,
    You have found a very simple, intuitive and nice solution. I am sure it will be useful for many other interested in process automatisation/control. Considering your ideas I have succeeded to obtain exactly what I was looking for and at the moment my code looks/should look as follows :
    Code:
    Private Sub Form_Load()
        Timer1.Interval = 60000 '60 seconds
    End Sub
    
    Private Sub Timer1_Timer()
        Static t As Byte   'keeps the counter live outside of routine !
        If Hour(Now) > 12 And Hour(Now) < 18 Then 'the time interval checked
               t = t + 1   'every minute t increases by 1
            If t = 15 Then 'after 15 minute we can call main task
               Call checkFolder
               t = 0       'reset t to start another 15 minute cycle
            End If
        End If
    End Sub
    
    Private Sub checkFolder()
           'check if some files exist in a folder
    End Sub
    Thank you Sam for your main contribution. Thank you too DataMiser for your valuable suggestions.

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