+ Post New Thread
Results 1 to 4 of 4
Coding Thread, Shutdown Service VB.NET - CLOSED in Coding and Web Development; I'm trying to make a service that will shutdown the computer at a certain time in the day so once ...
  1. #1
    thesk8rjesus's Avatar
    Join Date
    Sep 2008
    Posts
    107
    Thank Post
    11
    Thanked 6 Times in 6 Posts
    Rep Power
    14

    Shutdown Service VB.NET - CLOSED

    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

    Code:
    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
    Last edited by thesk8rjesus; 14th February 2012 at 02:47 PM.

  2. #2

    AngryTechnician's Avatar
    Join Date
    Oct 2008
    Posts
    3,730
    Thank Post
    698
    Thanked 1,214 Times in 761 Posts
    Rep Power
    395
    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?

  3. #3
    thesk8rjesus's Avatar
    Join Date
    Sep 2008
    Posts
    107
    Thank Post
    11
    Thanked 6 Times in 6 Posts
    Rep Power
    14
    I have just tried setting it to hours and minutes only but still nothing happens.

    Quote Originally Posted by AngryTechnician View Post
    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

  4. #4
    thesk8rjesus's Avatar
    Join Date
    Sep 2008
    Posts
    107
    Thank Post
    11
    Thanked 6 Times in 6 Posts
    Rep Power
    14
    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
    Code:
    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



SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 1
    Last Post: 25th May 2011, 02:14 PM
  2. VB.net get free space
    By Quackers in forum Coding
    Replies: 2
    Last Post: 16th July 2008, 02:56 PM
  3. Replies: 8
    Last Post: 3rd June 2008, 11:23 AM
  4. VB.Net running on your network?
    By Andi in forum Windows
    Replies: 9
    Last Post: 7th November 2007, 09:40 AM
  5. VB.Net Help
    By mloveless in forum Coding
    Replies: 2
    Last Post: 29th June 2007, 05:24 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •