Computer shutdown timer
Posted by austinslik on November 21, 2010
There are times when I’m just too tired to seat and wait for my downloads or a running program to finish before i turnoff my computer. Search the internet, i found Stevepuri shutdown timer created in Windows Forms (i learned a lot from his application).I decided to create one myself since I’m learning WPF (Windows presentation foundation).
Computer shutdown timer allows you to run Windows power options (Hibernate,LogOff,Restart,PowerOff,Sleep and Shutdown) by selecting a date and time. If the checkbox “Force shutdown” is clicked, the option Force shutdown will be added to the dropdown list. choosing another power option will remove the “Force shutdown” option.
WARNING: The “Force Shutdown” will close all running program. All unsaved data will be lost.
Thanks to mentalist.org for providing the windowsController.cs class file.
Here is the setup file for installation.
To force shutdown, check the “Force shutdown” checkbox and option will appear after accepting the warning Info about forcing shutdown
Source Code
namespace Computer_ShutDown
{
public partial class Window1 : Window
{
#region Fields
DispatcherTimer _timer;
DispatcherTimer _dateTimeNow;
WindowsController _winController;
ObservableCollection<string> _dropDownValue;
#endregion
#region Constructor
public Window1()
{
InitializeComponent();
_timer = new DispatcherTimer();
_dateTimeNow = new DispatcherTimer();
_winController = new WindowsController();
_dropDownValue = new ObservableCollection<string>();
powerOptionsNames();
_optionCombo.ItemsSource = dropDownValue;
dateTimePicker.SelectedDateTimeChanged +=
new DateTimeSelectedChangedRoutedEventHandler(dateTimePicker_SelectedDateTimeChanged);
this.Icon = BitmapFrame.Create(
new Uri("pack://application:,,,/Resources/press.png", UriKind.RelativeOrAbsolute));
#region _optionCombo.SelectionChanged +=
_optionCombo.SelectionChanged +=
new SelectionChangedEventHandler(delegate(object o, SelectionChangedEventArgs e)
{
if (_ForceShutdown.IsChecked == true && _optionCombo.SelectedIndex != 5)
{
_ForceShutdown.IsChecked = false;
_dropDownValue.RemoveAt(5);
}
_shutdownTimeTxt.Text += ":";
});
#endregion
}
#endregion
#region Properties
public ObservableCollection<string> dropDownValue
{
get { return _dropDownValue; }
}
#endregion
#region powerOptionsNames
private void powerOptionsNames()
{
foreach (string item in Enum.GetNames(typeof(PowerOptions)))
{
if(item != "ShutDown")
_dropDownValue.Add(item);
}
}
#endregion
#region ShutDown
public void ShutDown()
{
//close the application before power options
Application.Current.Shutdown();
//if checked force shutdown else do the rest
if (_ForceShutdown.IsChecked == true)
{
WindowsController.ExitWindows(PowerOptions.ShutDown, true);
}
else
{
switch (this._optionCombo.SelectedIndex)
{
case 0:
WindowsController.ExitWindows(PowerOptions.LogOff, false);
break;
case 1:
WindowsController.ExitWindows(PowerOptions.Restart, false);
break;
case 2:
WindowsController.ExitWindows(PowerOptions.PowerOff, false);
break;
case 3:
WindowsController.ExitWindows(PowerOptions.Hibernate, false);
break;
case 4:
WindowsController.ExitWindows(PowerOptions.Sleep, false);
break;
}
}
}
#endregion
#region dateTimePicker_SelectedDateTimeChanged
private void dateTimePicker_SelectedDateTimeChanged(
object sender,
DateTimeSelectedChangedRoutedEventArgs e)
{
this._dateTimeNowTxtBlock.Text = DateTime.Now.ToString();
this._shutDownTime.Text = e.NewDate.ToString();
}
#endregion
#region IsValidInput
private bool IsValidInput()
{
if (dateTimePicker.DateTimeSelected.CompareTo(DateTime.Now) < 0)
{
if (CustomMessageBox.Show(
_optionCombo.SelectedValue +
" time must be in the future. \n" +
"Please select another Date / Time", "Warning",
CustomMessageBoxButton.Yes,
CustomMessageBoxImage.Error,
" OK ", "") == CustomMessageBoxResult.OK)
{ this.dateTimePicker.DateTimeSelected = DateTime.Now; }
return false;
}
else
return true;
}
#endregion
#region Invoke_Click
private void Invoke_Click(
object sender,
RoutedEventArgs e)
{
if (IsValidInput())
{
this._ForceShutdown.IsEnabled = false;
this.dateTimePicker.IsEnabled = false;
this._optionCombo.IsEnabled = false;
this._invokeBtn.IsEnabled = false;
this._shutDownTime.Text = new DateTime(
dateTimePicker.DateTimeSelected.Year,
dateTimePicker.DateTimeSelected.Month,
dateTimePicker.DateTimeSelected.Day,
dateTimePicker.DateTimeSelected.Hour,
dateTimePicker.DateTimeSelected.Minute,
dateTimePicker.DateTimeSelected.Second).ToString();
_timer.Start();
_timer.Interval = new TimeSpan(0, 0, 1);
_timer.Tick += new EventHandler(delegate(object s, EventArgs a)
{
if (this._dateTimeNowTxtBlock.Text == dateTimePicker.DateTimeSelected.ToString())
{
_timer.Stop();
_dateTimeNow.Stop();
ShutDown();
}
});
}
}
#endregion
#region Cancel_Click
private void Cancel_Click(
object sender,
RoutedEventArgs e)
{
Application.Current.Shutdown();
}
#endregion
#region Window_Loaded
private void Window_Loaded(
object sender,
RoutedEventArgs e)
{
_dateTimeNow.Start();
_dateTimeNow.Interval = new TimeSpan(0, 0, 1);
_dateTimeNow.Tick += new EventHandler(delegate(object s, EventArgs a)
{
this._dateTimeNowTxtBlock.Text = DateTime.Now.ToString();
});
_shutdownTimeTxt.Text += ":";
}
#endregion
#region _bruteCheckBox_Click
private void _bruteCheckBox_Click(
object sender,
RoutedEventArgs e)
{
if (_ForceShutdown.IsChecked == true)
{
if (CustomMessageBox.Show(
"WARNING: Forcing Shutdown. \n\n" +
"All running programs will be closed\n\n" +
"Do you want to continue?", "Force Shutdown",
CustomMessageBoxButton.YesCancel,
CustomMessageBoxImage.Warning,
"OK", "Cancel") != CustomMessageBoxResult.OK)
{
_ForceShutdown.IsChecked = false;
_optionCombo.SelectedIndex = 0;
return;
}
else
{
_dropDownValue.Add("Force ShutDown");
_optionCombo.SelectedIndex = 5;
}
}
else
{
_dropDownValue.RemoveAt(5);
_optionCombo.SelectedIndex = 0;
}
}
#endregion
}
}







