Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / C# 5.0
Tip/Trick

C# Windows Shutdown with Shutdown Timer, Fast Shutdown, Restart, Lock, Sleep, Hibernate

Rate me:
Please Sign up or sign in to vote.
3.13/5 (6 votes)
30 Nov 2014CPOL 36.4K   1.1K   18   7
Shutdown, restart, logoff, lock, sleep, hibernate windows using C# and WPF

Introduction

This tip discusses C# WPF application with Windows shutdown timer, fast shutdown, normal shutdown, hibernate, sleep, lock, log off, restart options.

A preview

Using the Code

Add these namespaces:

C#
using System.Runtime.InteropServices;
using System.Threading.Tasks;

Creating a Countdown timer.

Intialization:

C#
 public static int timeLeft;
 public static int hour;
 public static int min;
 public static int sec;
 System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

timer.Interval = 1000; // Time Interval
timer.Tick += new EventHandler(timer_Tick); //Raising Event on each tick

Get Values of time variable and start timer:

C#
timeLeft = hour * 3600 + min * 60 + sec; // Calculate Timeleft in seconds
timer.Start(); //Starts timer

Countdown Timer with Each timer tick and shutdown when time is over:

C#
private void timer_Tick(object sender, EventArgs e)
        {
            if(timeLeft>0) // 
            {
                timeLeft = timeLeft - 1; // decrement of timeleft on each tick
                hour = timeLeft / 3600; // Left hours
                min = (timeLeft - (hour*3600)) / 60; //Left Minutes
                sec = timeLeft - (hour * 3600) - (min * 60); //Left Seconds
                hh.Text = hour.ToString(); // Setting hour Text on each timer tick
                mm.Text = min.ToString(); // Setting minutes Text on each timer tick
                ss.Text = sec.ToString(); // Setting sec Text on each timer tick
            }
            else
            {
                timer.Stop(); // Stop Timer
                Process.Start("shutdown", "/s /t 0"); // Shutdown PC when Time is over
            }
        }

To cancel timer use:

C#
timer.Stop( );

To pause timer use:

C#
timer.Pause( );

Other Windows Shutdown Options

1. Normal Shut down

C#
Process.Start("shutdown", "/s /t 0");

2. Fast Shut down Forcibly

C#
Process.Start("shutdown", "/s /f /t 0");

3. Restart

C#
Process.Start("shutdown", "/r /t 0");

4. Signout and Lock

Need to import already defined "user32" library and add these extern methods:

C#
[DllImport("user32")]
 public static extern void LockWorkStation(); // For Lock
 [DllImport("user32")]
 public static extern bool ExitWindowsEx(uint Flag, uint Reason); //For signout

SignOut

C#
ExitWindowsEx(0, 0);

Lock

C#
LockWorkStation();

5. Hibernate and Sleep

Need to import already defined "Powerprof" library and add this extern method:

C#
[DllImport("Powrprof.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);

Hibernate

C#
SetSuspendState(true, true, true);

Sleep

C#
SetSuspendState(false, true, true);

License

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


Written By
Student
India India
Learning never Ends!
And I m still learning.............

Comments and Discussions

 
Questiondont work by GPO Pin
Member 1348546825-Oct-17 10:38
Member 1348546825-Oct-17 10:38 
QuestionWindows Shutdown with Shutdown Timer, Fast Shutdown, Restart, Lock, Sleep, Hibernate Pin
Member 1312882114-Apr-17 8:01
Member 1312882114-Apr-17 8:01 
GeneralMy vote of 4 Pin
Vivek Kachhwaha4-Dec-14 18:44
Vivek Kachhwaha4-Dec-14 18:44 
Question[My vote of 1] Silly and big Pin
ozbear2-Dec-14 11:42
ozbear2-Dec-14 11:42 
AnswerRe: [My vote of 1] Silly and big Pin
Virender Prajapati5-Dec-14 2:25
professionalVirender Prajapati5-Dec-14 2:25 
QuestionNice tool, but timer hmmm... Pin
johannesnestler1-Dec-14 3:25
johannesnestler1-Dec-14 3:25 
AnswerRe: Nice tool, but timer hmmm... Pin
Virender Prajapati1-Dec-14 17:17
professionalVirender Prajapati1-Dec-14 17:17 

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.