-
Sep 18th, 2020, 02:07 PM
#1
Thread Starter
New Member
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.
-
Sep 18th, 2020, 02:44 PM
#2
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.
-
Sep 18th, 2020, 03:42 PM
#3
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
-
Sep 18th, 2020, 10:44 PM
#4
Re: Timed function call
Originally Posted by David52
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.
-
Sep 19th, 2020, 10:18 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|