Friday, November 13, 2009

System Shut Down Utility


This is a very light utility that I coded in just a few hours. It's purpose is to shut down the computer after a selected amount of time from the combo boxes (between 5 minutes to over 6 hours). The simple .exe illustrates a few basic but useful VB techniques:
  • Setting up combo boxes & values
  • Global variables
  • A timer
  • Time / Date / Now functions
  • Call functions from other subroutines
  • and the main feature, automatically shut down the computer
Instructions:
  • Download and unzip the package. Rename if necessary (see Final Tricks, below)
  • Open the program. The current date and time will display
  • Choose an amount of time from the Hours, and Minutes menus
  • Click "Commit" when ready. You will be prompted to verify (Yes or No) that you are ready to begin, choose Yes to continue
  • The time will be updated to report when the count began, and the remaining time (in minutes) will update every 60 seconds. When the time expires, the computer will automatically shut down. That's all there is to it!
Here are the guts of it:

Combo Boxes:
First, the contents of the pull down / combo boxes are set up in the Form_Load() subroutine, thus the contents of each menu are loaded when the app starts. Here's the code for the minutes list:

Private Sub Form_Load()
cboMins.AddItem ""
cboMins.ItemData(cboMins.NewIndex) = 0
cboMins.AddItem "5"
cboMins.ItemData(cboMins.NewIndex) = 5
cboMins.AddItem "15"
cboMins.ItemData(cboMins.NewIndex) = 15
cboMins.AddItem "30"
cboMins.ItemData(cboMins.NewIndex) = 30
cboMins.AddItem "45"
cboMins.ItemData(cboMins.NewIndex) = 45
'...
End Sub

Global Variables:
When the command button is clicked to run the program, the values from each of the combo boxes are passed to the intHours/intMinutes variables which have been dimensioned in the General Declarations (the topmost portion of the code editor). These can be called by various other functions to verify that a time has been selected (intHours + intMinutes > 0?), or to set the iterations of the timer, etc.

Option Explicit
Dim intHours As Integer
Dim intMinutes As Integer
_____________________________

Private Sub Form_Load()
'...

The VB Timer, Shutdown, & Calling Subroutines:
Timers can be a little tricky in this situation, but they're basically simple. There are two main properties: Enabled (on/off), and Interval (how long the timer runs). It would be great to set the timer to run for 4 hours and 5 minutes, but that's not how it works. The timer runs for a given number of milliseconds (1,000 milliseconds = 1 second) between 0 and 65,535 (just over 1 minute, 5 seconds). Instead we'll make a variable that will represent the total number of desired minutes, and loop through ever second (tmrShutDown.Interval = 60000) until the counter = 0; then run the desired procedure.

First, add a Timer control (Visual Basic 6) to the form named tmrShutDown. In Form_Load(), set the timer's Interval to 60000 (one minute) and make sure the timer is off as the app loads.

Private Sub Form_Load()
tmrShutDown.Enabled = False
tmrShutDown.Interval = 60000
'...
End Sub

Before the timer is enabled (via the command button), set a predefined global variable (intTime) to equal the amount of desired time in minutes. Now tell the timer to start running. Now when the timer is enabled, some code will run inside the timer's subroutine every 60 seconds (or 60,000 milliseconds); essentially as a loop. This will reiterate every 60 seconds until it is told to stop (tmrControl.Enabled = False). Insert a simple if-then statement: If the time counter (intTime) = zero, stop the timer and call the procedure to turn off the computer, otherwise, subtract one from the counter and continue.

Option Explicit
Dim intHours As Integer
Dim intMinutes As Integer
Dim intTime As Integer
___________________________________________

Private Sub cmdCommit_Click()
'...
intTime = (intHours * 60) + intMinutes
tmrShutDown.Enabled = True
'...
End Sub
___________________________________________

Private Sub tmrCommand_Timer()

If intTime = 0 Then
tmrCommand.Enabled = False 'Turn off timer
Call ShutDown
Else
intTime = intTime - 1
End If End Sub
___________________________________________

Private Sub ShutDown()
Shell "shutdown -s -f -t 00"
'...
End Sub


Final Tricks:


You might notice the odd title. Combined that with a similar inconspicuous file name alteration, and the app can go quite unnoticed on a computer - which might be useful to certain users.

Clicking the command button disables itself and both combo boxes to let the user know that it is working.

Finally, two labels are updated to include a time that the timer started, and how much time is left before shutdown. This should be updated in the timer's subroutine.

Private Sub cmdCommit_Click()
'...
cboHours.Enabled = False
cboMinutes.Enabled =
False
cmdCommit.Enabled = False

lblInitial.Caption = "Initial Time: " & Now
lblTimeLeft.Caption = "Time Remaining: " & intTime

End Sub

No comments: