Click here to Skip to main content
Licence CPOL
First Posted 23 Apr 2006
Views 34,923
Bookmarked 16 times

Shutdown windows after specified time

By | 23 Apr 2006 | Article
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)

About the Author

aliraza_se

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThis only works with XP Pinmemberaliraza_se19:20 24 Apr '06  
GeneralFor non XP peeps PinmemberEnnis Ray Lynch, Jr.3:58 24 Apr '06  
QuestionRe: For non XP peeps PinmemberChris Meech4:19 24 Apr '06  
AnswerRe: For non XP peeps PinmemberEnnis Ray Lynch, Jr.8:12 24 Apr '06  
GeneralNice! PinmemberVertyg023:43 23 Apr '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 23 Apr 2006
Article Copyright 2006 by aliraza_se
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid