Results 1 to 5 of 5

Thread: Timed function call

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    10

    Question Timed function call

    Hey! I want my app to do a backup save at a specified time(e.g 01:00 AM) everyday. How can I time my function to be called every night at 01:00, without timer, or isn't it possible ?I think the nonstop timer ticking would slow the app. My application'll be running non-stop.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Timed function call

    Something like this might work
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim t As Task
            t = Task.Run(Sub()
                             Do
                                 Dim whenToRun As TimeSpan
                                 'calculate when to run approx.
                                 whenToRun = DateTime.Now.Date.AddDays(1).AddHours(1).AddSeconds(-15) - DateTime.Now
                                 Threading.Thread.Sleep(whenToRun)
                                 While DateTime.Now.Hour < 1 'now wait for clock to get to 1
                                     Threading.Thread.Sleep(5000) 'set as needed for accuracy
                                 End While
                                 'backup code here or backup method call
                             Loop
                         End Sub)
        End Sub
    Last edited by dbasnett; Sep 21st, 2020 at 08:22 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Timed function call

    Technically, that could miss by up to five seconds, but hopefully that won't hurt all that much.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Timed function call

    Quote Originally Posted by David52 View Post
    without timer, or isn't it possible ?I think the nonstop timer ticking would slow the app.
    Firstly, no it wouldn't. Secondly, why would there be any non-stop Timer Ticking anyway. Put some thought into it. When you use an alarm clock, does it go off every few seconds so you can check the time and see whether you need to get up? Of course not. You set it to go off once at the exact time you need to get up. Why would you think that it should be any different with a Timer in code? When you start your app, you calculate how long it is until 1.00 AM and that's what yo set the Interval of the Timer to. Once it Ticks, you can either assume that it's accurate and set the Interval to 24 hours or account for any slight drift that may have happened and perform the same calculation again.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Timed function call

    The CPU manages waiting in a VERY efficient manner, so don't worry about the cost of waiting for some event to occur. You can assume that there isn't one.
    My usual boring signature: Nothing

Tags for this Thread

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