creating a service to shutdown machine

thesk8rjesus

New member
Joined
Feb 14, 2012
Messages
1
Programming Experience
Beginner
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


VB.NET:
Imports System
Imports System.Data
Imports System.Threading.Timer
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

EDIT:

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
VB.NET:
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
 
Last edited:
Back
Top