65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.13/5 (6 votes)

Dec 1, 2014

CPOL
viewsIcon

37433

downloadIcon

1115

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:

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

Creating a Countdown timer.

Intialization:

 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:

 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:

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:

timer.Stop( );

To pause timer use:

timer.Pause( );

Other Windows Shutdown Options

1. Normal Shut down

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

2. Fast Shut down Forcibly

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

3. Restart

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

4. Signout and Lock

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

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

SignOut

ExitWindowsEx(0, 0);

Lock

LockWorkStation();

5. Hibernate and Sleep

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

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

Hibernate

SetSuspendState(true, true, true);

Sleep

SetSuspendState(false, true, true);