问题
I am very new to the windows services. and am trying to create an application that will update the db nightly.
i already have the code that works (for the update itself)
but i can't figure out how to code it within the service.
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Dim timerDelegate As New TimerCallback(UpdateDB)
serviceTimer = New Timer(timerDelegate, Nothing, 0, 20000)
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
End Sub
as far as i understand, the above code will rune very 20 seconds, right? how can i code it so it only runs once a day... at let's say midnight.
thank you!
or should i be using a web service? can i schedule that?
回答1:
Do you need to use a windows service?
A simpler approach would be to create a console application and use the windows scheduler to launch it.
Windows services can be a pain to install and debug so I would only use them when absolutely necessary.
回答2:
A possible solution would be to have your timer execute on a 1000ms
interval (depending on how exact/close you need to be to actual midnight). Obviously, the lower your interval, the closer you'll be to getting exactly to midnight.
You could increase this, to say, every 5 minutes if you wanted less activity to occur within the timer, and if your threshold for execution could be +5 minutes after midnight.
Each time your timer fires, you'd then check the current time against the time you are attempting to fire an event off at, in this case midnight.
If the current time is midnight, or within the threshold of your defined of midnight, then execute your code.
来源:https://stackoverflow.com/questions/6749026/nightly-update-db-with-windows-service