I'm trying to make a service that will shutdown the computer at a certain time in the day so once everyone has gone home the machines aren't left on over night. I am using Visual Studio 2010 Ultimate to create a service that runs even if the machines logged off. I have been able to create the service and it runs, but when it gets to the time I set up the top it does nothing. Any help with this would be great
Public Class Service1
'Setting time to shutdown macine
Private AlarmTime As Date = "17:00:00"
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.
'Enabling Timer - tmrUp
tmrUp.Enabled = True
AddHandler tmrUp.Tick, AddressOf tmrUp_Tick
tmrUp.Interval = 1000
Catch ex As Exception
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
tmrUp.Enabled = False
Private Sub tmrUp_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUp.Tick
If AlarmTime.Hour = Now.Hour And AlarmTime.Minute = Now.Minute And AlarmTime.Second = Now.Second Then
Process.Start("shutdown", "-r -f -t 0")
Last edited by thesk8rjesus; 14th February 2012 at 02:47 PM.
I suspect it may be that the events aren't running often enough to catch the exact second that you're looking for, as the timer event only runs itself one per second. If the event is just 1ms late (which is perfectly plausible), you'll miss your target.
Lose the seconds check and just evaluate whether the hour and minute are the same. You can fire your timer less often that way too (maybe once every 30s).
I have to ask though, why not just use a scheduled task to do this?
Okay so if anyone else has this problem I have just solved it. The problem i was having was that the timer was for a windows form not a service, i added the service timer and now its working this also means that it's not longer a tick but elapse
Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
If AlarmTime.Hour = Now.Hour And AlarmTime.Minute = Now.Minute Then