Click here to Skip to main content
15,900,511 members
Articles / Programming Languages / Visual Basic
Article

ShutDown Timer

Rate me:
Please Sign up or sign in to vote.
1.46/5 (15 votes)
12 Aug 2006CPOL 64.1K   2.7K   23   6
Set timer to Turn Off your PC

Interface

Introduction

Before going in detail about the application.

This simple programme help you if you want to set timer to turn off your PC after spacified period.

Core behined Code

The most important code in this programme

System.Diagnostics.Process.Start("Shutdown", "/s")

I pass ' Shutdown ' to turn off .

So how am I achieving it? (What is the code behind it?)

first I used timer of course to increese variables every Tick of timer :-

This snap shot of code

this global var
Dim x,min,cou,hh As Integer = 0


Private Sub Timer1_Tick

'For each tick

'here I make counter

'x second

x = x + 1

If x = 60 Then

x = 0

min = min + 1

ProgressBar1.Value = ProgressBar1.Value + 1

End If

If min = 60 Then

hh += 1

min = 0

End If

Label5.Text = hh

'to display the timer

Label4.Text = x

Label3.Text = min

'NumericUpDown1 Input of hour

'NumericUpDown1 Input of hour
'NumericUpDown1 Input of hour

Dim tothor As Integer = Int32.Parse(NumericUpDown1.Value)

Dim totmin As Integer = Int32.Parse(NumericUpDown2.Value)

tothor = tothor * 60

Dim total As Double = tothor + totmin

'every tick check if it we achieve total of Minute

Dim totallabel As Double = hh * 60 + min

If totallabel = total Then

cou += 1

wa()

End If

End Sub

Now You can shut down

This sub shutdown pc

VB.NET
Public Sub wa()

If cou = 1 Then

Me.Show()

System.Diagnostics.Process.Start("Shutdown", "/s")

cou += 1

End If

End Sub

Finaly

pop up window will show give 30 second before shutting down.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Jordan Jordan
- Need is the mother of the invitation .
- To be acknowledge is matter of time..
be skillfull..needs...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Muhammady18-Mar-12 10:49
Muhammady18-Mar-12 10:49 
GeneralMy vote of 5 Pin
versx1-Aug-10 21:37
versx1-Aug-10 21:37 
Joke:( Pin
The_Mega_ZZTer13-Aug-06 9:02
The_Mega_ZZTer13-Aug-06 9:02 
"The most important code in this programme

System.Diagnostics.Process.Start("Shutdown", "/s")

I pass ' Shutdown ' to turn off your pc you can replace it 'log off' to log off your window"

OK this is so wrong it's funny. There is no "log off.exe".
C:\Documents and Settings\The MAZZTer>shutdown /?
Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "comment"] [-d up:xx:yy]

        No args                 Display this message (same as -?)
        -i                      Display GUI interface, must be the first option
        -l                      Log off (cannot be used with -m option)
        -s                      Shutdown the computer
        -r                      Shutdown and restart the computer
        -a                      Abort a system shutdown
        -m \\computername       Remote computer to shutdown/restart/abort
        -t xx                   Set timeout for shutdown to xx seconds
        -c "comment"            Shutdown comment (maximum of 127 characters)
        -f                      Forces running applications to close without warning
        -d [u][p]:xx:yy         The reason code for the shutdown
                                u is the user code
                                p is a planned shutdown code
                                xx is the major reason code (positive integer less than 256)
                                yy is the minor reason code (positive integer less than 65536)

C:\Documents and Settings\The MAZZTer>

You would launch shutdown with the /l parameter to log off. The /f parameter can be used to force all running programs to quit (it will kill off processes automatically that won't close) avoiding the "End Task" dialogs and overall speeding up the shutdown process (some programs might not save user settings).

Also the /t parameter shows that your program's functionality is built-in.

Here are some suggestions:
- Give your controls and functions meaningful names. "Label3" doesn't tell me anything, other than it's a label. "lHour" can tell me it's the label for the hour field. "wa" I assume is an acronym (unless it's in your native language)... which means nothing to me. "shutdownComputer" or "timesUp" or something would be more descriptive.
- Don't use global variables when local variables or some other mechanism would work fine. In this example, I might use the label texts for Hour, Minute, and Second to store the values. A more complicated solution would be to not use timers at all, but use a second thread with the Sleep() function and using .Invoke to control the GUI, although that's probably too complex for a simple program like this.
- Fonts. I noticed you picked a second font for some of the buttons and the label on top, it's not a very good GUI font as it's a bit hard to read. I find that the best way to make a GUI is to make GUIs that are similar to ones the users are already familiar with... Microsoft Windows standard dialogs (which look pretty good anyways).
- I see you have a cou variable. I'm guessing you had to use this because the timer kept ticking after the shutdown program was run. Instead, you should do:

Timer1.Enabled = false

When you want the timer to stop. Also since you ran the shutdown program, you can do one of these:

End<br />
Application.Exit()<br />
Form1.Close()


Any of these will end the program, which is going to happen anyways.



-- modified at 15:02 Sunday 13th August, 2006
GeneralRe: :(hi [modified] Pin
mr.stick13-Aug-06 23:23
mr.stick13-Aug-06 23:23 
Generalhe meant logoff Pin
Fabse_net14-Aug-06 4:02
Fabse_net14-Aug-06 4:02 
GeneralRe: he meant logoff Pin
mr.stick14-Aug-06 8:10
mr.stick14-Aug-06 8:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.