Imported 1.1.0.0 BETA 2
- Very crude implementation of continuous background ping.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace TimedShutdown
|
||||
{
|
||||
public class Messaging
|
||||
{
|
||||
public enum Message
|
||||
{
|
||||
RemoteShutdown
|
||||
}
|
||||
|
||||
public void Show(Message message, string info)
|
||||
{
|
||||
switch (message)
|
||||
{
|
||||
case Message.RemoteShutdown:
|
||||
MessageBox.Show(
|
||||
$"The following remote machine has been sent a shutdown/reboot signal:\n\n{info}",
|
||||
"Timed Shutdown",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Information);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Threading;
|
||||
|
||||
namespace TimedShutdown
|
||||
{
|
||||
public class PingWidget
|
||||
{
|
||||
IPAddress _ipAddress = IPAddress.None;
|
||||
public bool IsCancellationRequested { get; set; } = false;
|
||||
|
||||
BackgroundWorker contPingBackgroundWorker = new BackgroundWorker();
|
||||
|
||||
public void InitatePingRequest(IPAddress ipAddress)
|
||||
{
|
||||
_ipAddress = ipAddress;
|
||||
|
||||
contPingBackgroundWorker.DoWork += ContPingBackgroundWorkerDoWork;
|
||||
contPingBackgroundWorker.RunWorkerAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will continuously ping the input IP address every 5 seconds,
|
||||
/// will stop if a cancellation request is made.
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void ContPingBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
|
||||
{
|
||||
|
||||
while (IsCancellationRequested == false)
|
||||
{
|
||||
Thread.Sleep(5000);
|
||||
|
||||
Ping pingSender = new Ping();
|
||||
IPAddress address = _ipAddress;
|
||||
PingReply reply = pingSender.Send(address);
|
||||
|
||||
if (reply.Status == IPStatus.Success)
|
||||
Debug.WriteLine("Ping Success!");
|
||||
else
|
||||
{
|
||||
Debug.WriteLine(reply.Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,5 +51,5 @@ using System.Windows;
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyVersion("1.1.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.1.0.0")]
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace TimedShutdown
|
||||
{
|
||||
public class ShutdownManager
|
||||
{
|
||||
private readonly Func<bool, string> _funcForceShutdown = ForceShutdown;
|
||||
private readonly Func<Operation, string> _funcShutdownOperation = ShutdownOperation;
|
||||
private readonly Func<string, string> _funcRemoteShutdown = RemoteShutdown;
|
||||
|
||||
public enum Operation
|
||||
{
|
||||
Shutdown,
|
||||
@@ -17,18 +22,16 @@ namespace TimedShutdown
|
||||
/// <param name="shutdownoperation"></param>
|
||||
/// <param name="shutdownSeconds"></param>
|
||||
/// <param name="forceShutdownState"></param>
|
||||
public void ExecuteOperation(Operation shutdownoperation, int shutdownSeconds, bool forceShutdownState)
|
||||
public void ExecuteOperation(Operation shutdownoperation, int shutdownSeconds, bool forceShutdownState, string remoteShutdown)
|
||||
{
|
||||
Func<bool,string> forceShutdown = ForceShutdown;
|
||||
Func<Operation, string> shutdownOperation = ShutdownOperation;
|
||||
|
||||
ProcessStartInfo info = new ProcessStartInfo();
|
||||
Process proc = new Process();
|
||||
|
||||
info.FileName = "cmd.exe";
|
||||
info.Arguments = $"/c shutdown {ShutdownOperation(shutdownoperation)} -t" +
|
||||
info.Arguments = $"/c shutdown {_funcShutdownOperation(shutdownoperation)} -t" +
|
||||
$" {shutdownSeconds}" +
|
||||
$" {ForceShutdown(forceShutdownState)}";
|
||||
$" {_funcForceShutdown(forceShutdownState)}" +
|
||||
$" {_funcRemoteShutdown(remoteShutdown)}";
|
||||
info.WindowStyle = ProcessWindowStyle.Hidden;
|
||||
info.CreateNoWindow = true;
|
||||
|
||||
@@ -36,22 +39,23 @@ namespace TimedShutdown
|
||||
proc.Start();
|
||||
}
|
||||
|
||||
public void AbortOperation()
|
||||
public void AbortOperation(string remoteShutdown)
|
||||
{
|
||||
ProcessStartInfo info = new ProcessStartInfo();
|
||||
Process proc = new Process();
|
||||
|
||||
info.FileName = "cmd.exe";
|
||||
info.Arguments = $"/c shutdown -a";
|
||||
info.Arguments = $"/c shutdown -a {_funcRemoteShutdown(remoteShutdown)}";
|
||||
info.CreateNoWindow = true;
|
||||
info.WindowStyle = ProcessWindowStyle.Hidden;
|
||||
|
||||
proc.StartInfo = info;
|
||||
proc.Start();
|
||||
}
|
||||
|
||||
#region Functions
|
||||
|
||||
private string ShutdownOperation(Operation arg)
|
||||
private static string ShutdownOperation(Operation arg)
|
||||
{
|
||||
switch (arg)
|
||||
{
|
||||
@@ -64,11 +68,21 @@ namespace TimedShutdown
|
||||
}
|
||||
}
|
||||
|
||||
private string ForceShutdown(bool arg)
|
||||
private static string ForceShutdown(bool arg)
|
||||
{
|
||||
return "-f";
|
||||
}
|
||||
|
||||
private static string RemoteShutdown(string arg)
|
||||
{
|
||||
if (string.IsNullOrEmpty(arg)) return string.Empty;
|
||||
if (arg.StartsWith(@"\\"))
|
||||
{
|
||||
return $"-m {arg}";
|
||||
}
|
||||
return $@"-m \\{arg}";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
@@ -70,6 +70,8 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Messaging.cs" />
|
||||
<Compile Include="PingWidget.cs" />
|
||||
<Compile Include="ShutdownManager.cs" />
|
||||
<Compile Include="ViewModels\ShellRootViewModel.cs" />
|
||||
<Compile Include="Views\ShellRootView.xaml.cs">
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Caliburn.Micro;
|
||||
|
||||
@@ -6,29 +8,8 @@ namespace TimedShutdown.ViewModels
|
||||
{
|
||||
public class ShellRootViewModel : Screen
|
||||
{
|
||||
readonly ShutdownManager shutdownManager = new ShutdownManager();
|
||||
|
||||
#region Labels
|
||||
|
||||
public string LabelWindowTitle { get; } = "Timed Shutdown";
|
||||
|
||||
public string LabelHoursLabel { get; } = "Hours:";
|
||||
|
||||
public string LabelMinutesLabel { get; } = "Minutes:";
|
||||
|
||||
public string LabelShutdownOptions { get; } = "Options:";
|
||||
|
||||
public string LabelShutdownOptionsForce { get; } = "Force Operation";
|
||||
|
||||
public string LabelShutdownTime { get; set; } = "Shutdown Time:";
|
||||
|
||||
public string LabelShutdown { get; } = "Shutdown";
|
||||
|
||||
public string LabelReboot { get; } = "Reboot";
|
||||
|
||||
public string LabelAbortShutdown { get; } = "Abort Shutdown";
|
||||
|
||||
#endregion
|
||||
private readonly ShutdownManager _shutdownManager = new ShutdownManager();
|
||||
private readonly PingWidget pingWidget = new PingWidget();
|
||||
|
||||
#region Values
|
||||
|
||||
@@ -82,6 +63,66 @@ namespace TimedShutdown.ViewModels
|
||||
|
||||
public bool ShutdownOptionsForce { get; set; }
|
||||
|
||||
private string _remoteShutdown;
|
||||
public string RemoteShutdown
|
||||
{
|
||||
get { return _remoteShutdown; }
|
||||
set
|
||||
{
|
||||
// Show remote shutdown alert upon checking for a non-loopback
|
||||
// address.
|
||||
if (!string.IsNullOrEmpty(value)
|
||||
&& value != "localhost"
|
||||
&& value != "127.0.0.1")
|
||||
{
|
||||
Messaging alert = new Messaging();
|
||||
alert.Show(Messaging.Message.RemoteShutdown, value);
|
||||
}
|
||||
_remoteShutdown = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _pingRemoteHost;
|
||||
public bool PingRemoteHost
|
||||
{
|
||||
get { return _pingRemoteHost; }
|
||||
set
|
||||
{
|
||||
_pingRemoteHost = value;
|
||||
|
||||
if (value == true)
|
||||
{
|
||||
pingWidget.IsCancellationRequested = false;
|
||||
pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown));
|
||||
}
|
||||
else
|
||||
{
|
||||
pingWidget.IsCancellationRequested = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string PingRemoteHostStatus { get; set; }
|
||||
|
||||
#region Remote Expander
|
||||
|
||||
public int WindowHeight { get; set; } = 335; // Default 335
|
||||
|
||||
private bool _remoteExpander;
|
||||
public bool RemoteExpander
|
||||
{
|
||||
get { return _remoteExpander; }
|
||||
set
|
||||
{
|
||||
WindowHeight = value ? 460 : 335;
|
||||
NotifyOfPropertyChange(() => WindowHeight);
|
||||
|
||||
_remoteExpander = value;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#region Buttons
|
||||
@@ -89,44 +130,48 @@ namespace TimedShutdown.ViewModels
|
||||
public bool CanShutdown { get; set; } = true;
|
||||
public void Shutdown()
|
||||
{
|
||||
shutdownManager.ExecuteOperation(ShutdownManager.Operation.Shutdown, ShutdownTime, false);
|
||||
CanAbortShutdown = true;
|
||||
CanReboot = false;
|
||||
CanShutdown = false;
|
||||
_shutdownManager.ExecuteOperation(
|
||||
ShutdownManager.Operation.Shutdown,
|
||||
ShutdownTime,
|
||||
ShutdownOptionsForce,
|
||||
RemoteShutdown);
|
||||
|
||||
NotifyOfPropertyChange(() => CanAbortShutdown);
|
||||
NotifyOfPropertyChange(() => CanReboot);
|
||||
NotifyOfPropertyChange(() => CanShutdown);
|
||||
CanReboot = false;
|
||||
CanShutdown = false;
|
||||
|
||||
NotifyOfPropertyChange(() => CanReboot);
|
||||
NotifyOfPropertyChange(() => CanShutdown);
|
||||
}
|
||||
|
||||
public bool CanReboot { get; set; } = true;
|
||||
public void Reboot()
|
||||
{
|
||||
shutdownManager.ExecuteOperation(ShutdownManager.Operation.Reboot, ShutdownTime, false);
|
||||
CanAbortShutdown = true;
|
||||
CanReboot = false;
|
||||
CanShutdown = false;
|
||||
_shutdownManager.ExecuteOperation(
|
||||
ShutdownManager.Operation.Reboot,
|
||||
ShutdownTime,
|
||||
ShutdownOptionsForce,
|
||||
RemoteShutdown);
|
||||
|
||||
NotifyOfPropertyChange(() => CanAbortShutdown);
|
||||
NotifyOfPropertyChange(() => CanReboot);
|
||||
NotifyOfPropertyChange(() => CanShutdown);
|
||||
CanReboot = false;
|
||||
CanShutdown = false;
|
||||
|
||||
NotifyOfPropertyChange(() => CanReboot);
|
||||
NotifyOfPropertyChange(() => CanShutdown);
|
||||
}
|
||||
|
||||
public bool CanAbortShutdown { get; set; } = true;
|
||||
public void AbortShutdown()
|
||||
{
|
||||
// Reset values to defaults.
|
||||
Minutes = 0;
|
||||
Hours = 0;
|
||||
|
||||
shutdownManager.AbortOperation();
|
||||
CanAbortShutdown = false;
|
||||
_shutdownManager.AbortOperation(RemoteShutdown);
|
||||
CanReboot = true;
|
||||
CanShutdown = true;
|
||||
|
||||
NotifyOfPropertyChange(() => CanAbortShutdown);
|
||||
NotifyOfPropertyChange(() => CanReboot);
|
||||
NotifyOfPropertyChange(() => CanShutdown);
|
||||
NotifyOfPropertyChange(() => RemoteShutdown);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
mc:Ignorable="d"
|
||||
Title="{Binding LabelWindowTitle}" Height="300" Width="400" WindowStartupLocation="CenterScreen"
|
||||
Title="Timed Shutdown" Height="{Binding WindowHeight, Mode=TwoWay}" Width="400" WindowStartupLocation="CenterScreen"
|
||||
ResizeMode="NoResize">
|
||||
<StackPanel>
|
||||
<Grid x:Name="ShutdownIcon" Margin="15">
|
||||
@@ -21,36 +21,37 @@
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Icon -->
|
||||
<Image Source="/TimedShutdown;component/Resources/ShutdownIcon.png" Grid.Column="0" Grid.Row="0"
|
||||
Margin="0,0,5,0" Grid.RowSpan="3" RenderOptions.BitmapScalingMode="Fant"/>
|
||||
Margin="0,0,5,0" Grid.RowSpan="5" RenderOptions.BitmapScalingMode="Fant"/>
|
||||
|
||||
<!-- Row 1: Hours -->
|
||||
<Label x:Name="LabelHoursLabel" Grid.Row="0" Grid.Column="1" />
|
||||
<Label Content="Hours:" Grid.Row="0" Grid.Column="1" />
|
||||
<Slider Value="{Binding Hours, Mode=TwoWay}" Grid.Row="0" Grid.Column="2"
|
||||
Margin="10,0,10,0" Maximum="23" TickPlacement="BottomRight"/>
|
||||
<TextBox Text="{Binding Hours, Mode=TwoWay}" Grid.Row="0" Grid.Column="3"
|
||||
Margin="0,2.5" Padding="5" />
|
||||
|
||||
<!-- Row 2: Minutes -->
|
||||
<Label x:Name="LabelMinutesLabel" Grid.Row="1" Grid.Column="1" />
|
||||
<Label Content="Minutes:" Grid.Row="1" Grid.Column="1" />
|
||||
<Slider Value="{Binding Minutes, Mode=TwoWay}" Grid.Row="1" Grid.Column="2"
|
||||
Margin="10,0,10,0" Maximum="59" TickPlacement="BottomRight"/>
|
||||
<TextBox Text="{Binding Minutes, Mode=TwoWay}" Grid.Row="1" Grid.Column="3"
|
||||
Margin="0,2.5" Padding="5"/>
|
||||
|
||||
<!-- Row 3: Options -->
|
||||
<Label x:Name="LabelShutdownOptions" Grid.Row="2" Grid.Column="1" />
|
||||
<CheckBox x:Name="ShutdownOptionsForce" Content="{Binding LabelShutdownOptionsForce}" Grid.Row="2" Grid.Column="2"
|
||||
Margin="10,6,0,0" />
|
||||
<Label Content="Options:" Grid.Row="2" Grid.Column="1" />
|
||||
<CheckBox x:Name="ShutdownOptionsForce" Content="Force Operation" Grid.Row="2" Grid.Column="2"
|
||||
Margin="10,6,0,0" ToolTip="Forces a system shutdown/restart by ignoring programs preventing a shutdown."/>
|
||||
|
||||
<!-- Row 4: Shutdown Time -->
|
||||
<Label x:Name="LabelShutdownTime" Grid.Row="4" Grid.Column="1" Margin="0,5,0,0"/>
|
||||
<Label Content="Shutdown Time:" Grid.Row="4" Grid.Column="1" Margin="0,5,0,0"/>
|
||||
<TextBox Text="{Binding ShutdownTimeSpan, Mode=OneWay}" Grid.Row="4" Grid.Column="2"
|
||||
Grid.ColumnSpan="2" Padding="5" Margin="10,5,0,0" IsEnabled="False"/>
|
||||
|
||||
|
||||
</Grid>
|
||||
|
||||
<!-- Row 5: Shutdown/Reboot -->
|
||||
@@ -64,15 +65,50 @@
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Button x:Name="Shutdown" Content="{Binding LabelShutdown}" Grid.Row="0" Grid.Column="0"
|
||||
<Button x:Name="Shutdown" Content="Shutdown" Grid.Row="0" Grid.Column="0"
|
||||
Padding="5" Margin="0,5,2.5,0"/>
|
||||
<Button x:Name="Reboot" Content="{Binding LabelReboot}" Grid.Row="0" Grid.Column="1"
|
||||
<Button x:Name="Reboot" Content="Reboot" Grid.Row="0" Grid.Column="1"
|
||||
Padding="5" Margin="2.5,5,0,0"/>
|
||||
<Button x:Name="AbortShutdown" Content="{Binding LabelAbortShutdown}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
|
||||
<Button x:Name="AbortShutdown" Content="Abort Shutdown" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
|
||||
Padding="5" Margin="0,5,0,0"/>
|
||||
|
||||
<!-- Remote Shutdown Options -->
|
||||
</Grid>
|
||||
<Separator/>
|
||||
<Expander IsExpanded="{Binding RemoteExpander}" x:Name="remoteExpander" Header="Remote Shutdown Options" Height="172" Padding="5">
|
||||
|
||||
<Grid>
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="1*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Row 6: Hostname/IP-->
|
||||
<Label Content="Hostname/IP Address:" Grid.Row="0" Grid.Column="0" Margin="0,5,0,0"/>
|
||||
<TextBox Text="{Binding RemoteShutdown, Mode=TwoWay}" Grid.Row="0" Grid.Column="1"
|
||||
Padding="5" Margin="10,5,5,0"
|
||||
ToolTip="Enter the machine name or IP address to remotely shutdown. NOTE: This will only work on machines part of the same Windows Domain."/>
|
||||
|
||||
<!-- Row 7: Ping remote host -->
|
||||
<Label Content="Options:" Grid.Row="1" Grid.Column="0"/>
|
||||
<CheckBox IsChecked="{Binding PingRemoteHost, Mode=OneWayToSource}" Content="Ping remote host"
|
||||
Grid.Row="1" Grid.Column="1" Margin="10,5,0,0"/>
|
||||
|
||||
<!-- Row 8: Ping status -->
|
||||
<Label Content="Status:" Grid.Row="2" Grid.Column="0"/>
|
||||
<Label Content="{Binding PingRemoteHostStatus}" Grid.Row="2" Grid.Column="1"/>
|
||||
|
||||
</Grid>
|
||||
</Expander>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user