thesk8rjesus Posted February 14, 2012 Posted February 14, 2012 (edited) 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 Imports System Imports System.Data Imports System.Threading.Timer Imports System.Diagnostics Imports System.ServiceProcess 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. Try 'Enabling Timer - tmrUp tmrUp.Enabled = True AddHandler tmrUp.Tick, AddressOf tmrUp_Tick tmrUp.Interval = 1000 tmrUp.Start() Catch ex As Exception End Try End Sub Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. tmrUp.Enabled = False End Sub 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") End If End Sub End Class Edited February 14, 2012 by thesk8rjesus
AngryTechnician Posted February 14, 2012 Posted February 14, 2012 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?
thesk8rjesus Posted February 14, 2012 Author Posted February 14, 2012 I have just tried setting it to hours and minutes only but still nothing happens. I have to ask though, why not just use a scheduled task to do this? I have done it as a service as we want to be able to run our custom shutdown program once I have this bit working. Thank you for your help
thesk8rjesus Posted February 14, 2012 Author Posted February 14, 2012 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 Process.Start("c:\windows\system32\shutdown.exe") End If End Sub
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now