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

Shutdown windows after specified time

Rate me:
Please Sign up or sign in to vote.
1.32/5 (12 votes)
23 Apr 2006CPOL 46.8K   534   16   5
Shutdown windows after specified minutes. You can also pause and resume shutdown timer. Implemented as an application, can be converted as windows service.

Sample Image - shutdownwin.jpg

Introduction

This is a quite simple few minutes build application for shutting down windows after specified minutes with some flare like pausing and resuming shutdown timer.

Imports System.Timers

Public Class ShutDownWin
    Inherits System.Windows.Forms.Form

    Private WinTimer As New Timer(1000)
    Private time As Integer
    Private TotalSec As Integer


    Private Sub SetClockTimer()
        //Activate Test Clock
        AddHandler WinTimer.Elapsed, AddressOf ClockTimerFired

        TotalSec = time * 60
        SetTimeStr()

        //Start Clock
        WinTimer.Enabled = True
    End Sub

    //For Displaying time
    Private Sub SetTimeStr()

        Dim Min As Integer = Math.Ceiling(CDbl(TotalSec) / 60)
        Dim Sec As Integer = TotalSec Mod 60

        If Sec = 0 Then
            If Min < 10 Then
                Me.lblTime.Text = ("0" & CStr(Min) & ":0" & CStr(Sec))
            Else
                Me.lblTime.Text = (CStr(Min) & ":0" & CStr(Sec))
            End If
        Else

            If (Min) < 11 Then
                If Sec < 10 Then
                    Me.lblTime.Text = ("0" & CStr(Min - 1) & ":0" & CStr(Sec))
                Else
                    Me.lblTime.Text = ("0" & CStr(Min - 1) & ":" & CStr(Sec))
                End If
            Else
                If Sec < 10 Then
                    Me.lblTime.Text = (CStr(Min - 1) & ":0" & CStr(Sec))
                Else
                    Me.lblTime.Text = (CStr(Min - 1) & ":" & CStr(Sec))
                End If
            End If

        End If

    End Sub

    Public Sub ClockTimerFired(ByVal sender As Object, ByVal e As ElapsedEventArgs)

        TotalSec -= 1
        Me.SetTimeStr()

        If TotalSec < 1 Then
            'Me.StopTestTimer()
            WinTimer.Stop()
            WinTimer.Enabled = False

            TotalSec = 0
            SetTimeStr()

            //Shutdown windows
            'MsgBox("Shutdown commencing")
            System.Diagnostics.Process.Start("Shutdown", "/s")
        End If
    End Sub

    Private Sub btnActivate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActivate.Click

        btnDeactivate.Enabled = True
        btnPause.Enabled = True
        lblTLeft.Enabled = True
        lblTime.Visible = True
        btnActivate.Enabled = False

        time = numTime.Value
        SetClockTimer()
    End Sub

    Private Sub btnDeactivate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeactivate.Click

        btnActivate.Enabled = True
        btnPause.Enabled = False
        btnPause.Text = "Pause"
        lblTLeft.Enabled = False
        lblTime.Visible = False
        btnDeactivate.Enabled = False

        //Stop timer
        WinTimer.Stop()
        WinTimer.Enabled = False

    End Sub

    Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click

        If btnPause.Text = "Pause" Then

            'btnActivate.Enabled = False
            btnPause.Text = "Resume"
            lblTLeft.Enabled = False
            lblTime.Enabled = False
            'btnDeactivate.Enabled = False

            WinTimer.Stop()
            WinTimer.Enabled = False

        Else

            'btnActivate.Enabled = False
            btnPause.Text = "Pause"
            lblTLeft.Enabled = True
            lblTime.Enabled = True
            'btnDeactivate.Enabled = False

            WinTimer.Start()
            WinTimer.Enabled = True

        End If

    End Sub


    Private Sub numTime_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles numTime.KeyPress
        e.Handled = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

End Class

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThis only works with XP Pin
aliraza_se24-Apr-06 19:20
aliraza_se24-Apr-06 19:20 
GeneralFor non XP peeps Pin
Ennis Ray Lynch, Jr.24-Apr-06 3:58
Ennis Ray Lynch, Jr.24-Apr-06 3:58 
QuestionRe: For non XP peeps Pin
Chris Meech24-Apr-06 4:19
Chris Meech24-Apr-06 4:19 
AnswerRe: For non XP peeps Pin
Ennis Ray Lynch, Jr.24-Apr-06 8:12
Ennis Ray Lynch, Jr.24-Apr-06 8:12 
GeneralNice! Pin
Vertyg023-Apr-06 23:43
Vertyg023-Apr-06 23:43 

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.