Imported 1.1.0.0 BETA 2

- Very crude implementation of continuous background ping.
This commit is contained in:
2019-03-05 21:03:37 +00:00
parent 9bb6d80782
commit 7192a2d3e8
10 changed files with 239 additions and 66 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.27130.2027 VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimedShutdown", "TimedShutdown\TimedShutdown.csproj", "{C1158C45-7666-4586-A6DA-CCC1EE95D457}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TimedShutdown", "TimedShutdown\TimedShutdown.csproj", "{C1158C45-7666-4586-A6DA-CCC1EE95D457}"
EndProject EndProject
+26
View File
@@ -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;
}
}
}
}
+50
View File
@@ -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);
}
}
}
}
}
+2 -2
View File
@@ -51,5 +51,5 @@ using System.Windows;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.1.0.0")]
+24 -10
View File
@@ -1,10 +1,15 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Net.Sockets;
namespace TimedShutdown namespace TimedShutdown
{ {
public class ShutdownManager 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 public enum Operation
{ {
Shutdown, Shutdown,
@@ -17,18 +22,16 @@ namespace TimedShutdown
/// <param name="shutdownoperation"></param> /// <param name="shutdownoperation"></param>
/// <param name="shutdownSeconds"></param> /// <param name="shutdownSeconds"></param>
/// <param name="forceShutdownState"></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(); ProcessStartInfo info = new ProcessStartInfo();
Process proc = new Process(); Process proc = new Process();
info.FileName = "cmd.exe"; info.FileName = "cmd.exe";
info.Arguments = $"/c shutdown {ShutdownOperation(shutdownoperation)} -t" + info.Arguments = $"/c shutdown {_funcShutdownOperation(shutdownoperation)} -t" +
$" {shutdownSeconds}" + $" {shutdownSeconds}" +
$" {ForceShutdown(forceShutdownState)}"; $" {_funcForceShutdown(forceShutdownState)}" +
$" {_funcRemoteShutdown(remoteShutdown)}";
info.WindowStyle = ProcessWindowStyle.Hidden; info.WindowStyle = ProcessWindowStyle.Hidden;
info.CreateNoWindow = true; info.CreateNoWindow = true;
@@ -36,22 +39,23 @@ namespace TimedShutdown
proc.Start(); proc.Start();
} }
public void AbortOperation() public void AbortOperation(string remoteShutdown)
{ {
ProcessStartInfo info = new ProcessStartInfo(); ProcessStartInfo info = new ProcessStartInfo();
Process proc = new Process(); Process proc = new Process();
info.FileName = "cmd.exe"; info.FileName = "cmd.exe";
info.Arguments = $"/c shutdown -a"; info.Arguments = $"/c shutdown -a {_funcRemoteShutdown(remoteShutdown)}";
info.CreateNoWindow = true; info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden; info.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo = info; proc.StartInfo = info;
proc.Start(); proc.Start();
} }
#region Functions #region Functions
private string ShutdownOperation(Operation arg) private static string ShutdownOperation(Operation arg)
{ {
switch (arg) switch (arg)
{ {
@@ -64,11 +68,21 @@ namespace TimedShutdown
} }
} }
private string ForceShutdown(bool arg) private static string ForceShutdown(bool arg)
{ {
return "-f"; 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 #endregion
} }
+2
View File
@@ -70,6 +70,8 @@
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="Messaging.cs" />
<Compile Include="PingWidget.cs" />
<Compile Include="ShutdownManager.cs" /> <Compile Include="ShutdownManager.cs" />
<Compile Include="ViewModels\ShellRootViewModel.cs" /> <Compile Include="ViewModels\ShellRootViewModel.cs" />
<Compile Include="Views\ShellRootView.xaml.cs"> <Compile Include="Views\ShellRootView.xaml.cs">
+86 -41
View File
@@ -1,4 +1,6 @@
using System; using System;
using System.Diagnostics;
using System.Net;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using Caliburn.Micro; using Caliburn.Micro;
@@ -6,29 +8,8 @@ namespace TimedShutdown.ViewModels
{ {
public class ShellRootViewModel : Screen public class ShellRootViewModel : Screen
{ {
readonly ShutdownManager shutdownManager = new ShutdownManager(); private readonly ShutdownManager _shutdownManager = new ShutdownManager();
private readonly PingWidget pingWidget = new PingWidget();
#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
#region Values #region Values
@@ -82,6 +63,66 @@ namespace TimedShutdown.ViewModels
public bool ShutdownOptionsForce { get; set; } 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 #endregion
#region Buttons #region Buttons
@@ -89,44 +130,48 @@ namespace TimedShutdown.ViewModels
public bool CanShutdown { get; set; } = true; public bool CanShutdown { get; set; } = true;
public void Shutdown() public void Shutdown()
{ {
shutdownManager.ExecuteOperation(ShutdownManager.Operation.Shutdown, ShutdownTime, false); _shutdownManager.ExecuteOperation(
CanAbortShutdown = true; ShutdownManager.Operation.Shutdown,
CanReboot = false; ShutdownTime,
CanShutdown = false; ShutdownOptionsForce,
RemoteShutdown);
NotifyOfPropertyChange(() => CanAbortShutdown); CanReboot = false;
NotifyOfPropertyChange(() => CanReboot); CanShutdown = false;
NotifyOfPropertyChange(() => CanShutdown);
NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown);
} }
public bool CanReboot { get; set; } = true; public bool CanReboot { get; set; } = true;
public void Reboot() public void Reboot()
{ {
shutdownManager.ExecuteOperation(ShutdownManager.Operation.Reboot, ShutdownTime, false); _shutdownManager.ExecuteOperation(
CanAbortShutdown = true; ShutdownManager.Operation.Reboot,
CanReboot = false; ShutdownTime,
CanShutdown = false; ShutdownOptionsForce,
RemoteShutdown);
NotifyOfPropertyChange(() => CanAbortShutdown); CanReboot = false;
NotifyOfPropertyChange(() => CanReboot); CanShutdown = false;
NotifyOfPropertyChange(() => CanShutdown);
NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown);
} }
public bool CanAbortShutdown { get; set; } = true;
public void AbortShutdown() public void AbortShutdown()
{ {
// Reset values to defaults. // Reset values to defaults.
Minutes = 0; Minutes = 0;
Hours = 0; Hours = 0;
shutdownManager.AbortOperation(); _shutdownManager.AbortOperation(RemoteShutdown);
CanAbortShutdown = false;
CanReboot = true; CanReboot = true;
CanShutdown = true; CanShutdown = true;
NotifyOfPropertyChange(() => CanAbortShutdown);
NotifyOfPropertyChange(() => CanReboot); NotifyOfPropertyChange(() => CanReboot);
NotifyOfPropertyChange(() => CanShutdown); NotifyOfPropertyChange(() => CanShutdown);
NotifyOfPropertyChange(() => RemoteShutdown);
} }
#endregion #endregion
+48 -12
View File
@@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" 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"> ResizeMode="NoResize">
<StackPanel> <StackPanel>
<Grid x:Name="ShutdownIcon" Margin="15"> <Grid x:Name="ShutdownIcon" Margin="15">
@@ -21,36 +21,37 @@
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<!-- Icon --> <!-- Icon -->
<Image Source="/TimedShutdown;component/Resources/ShutdownIcon.png" Grid.Column="0" Grid.Row="0" <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 --> <!-- 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" <Slider Value="{Binding Hours, Mode=TwoWay}" Grid.Row="0" Grid.Column="2"
Margin="10,0,10,0" Maximum="23" TickPlacement="BottomRight"/> Margin="10,0,10,0" Maximum="23" TickPlacement="BottomRight"/>
<TextBox Text="{Binding Hours, Mode=TwoWay}" Grid.Row="0" Grid.Column="3" <TextBox Text="{Binding Hours, Mode=TwoWay}" Grid.Row="0" Grid.Column="3"
Margin="0,2.5" Padding="5" /> Margin="0,2.5" Padding="5" />
<!-- Row 2: Minutes --> <!-- 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" <Slider Value="{Binding Minutes, Mode=TwoWay}" Grid.Row="1" Grid.Column="2"
Margin="10,0,10,0" Maximum="59" TickPlacement="BottomRight"/> Margin="10,0,10,0" Maximum="59" TickPlacement="BottomRight"/>
<TextBox Text="{Binding Minutes, Mode=TwoWay}" Grid.Row="1" Grid.Column="3" <TextBox Text="{Binding Minutes, Mode=TwoWay}" Grid.Row="1" Grid.Column="3"
Margin="0,2.5" Padding="5"/> Margin="0,2.5" Padding="5"/>
<!-- Row 3: Options --> <!-- Row 3: Options -->
<Label x:Name="LabelShutdownOptions" Grid.Row="2" Grid.Column="1" /> <Label Content="Options:" Grid.Row="2" Grid.Column="1" />
<CheckBox x:Name="ShutdownOptionsForce" Content="{Binding LabelShutdownOptionsForce}" Grid.Row="2" Grid.Column="2" <CheckBox x:Name="ShutdownOptionsForce" Content="Force Operation" Grid.Row="2" Grid.Column="2"
Margin="10,6,0,0" /> Margin="10,6,0,0" ToolTip="Forces a system shutdown/restart by ignoring programs preventing a shutdown."/>
<!-- Row 4: Shutdown Time --> <!-- 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" <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.ColumnSpan="2" Padding="5" Margin="10,5,0,0" IsEnabled="False"/>
</Grid> </Grid>
<!-- Row 5: Shutdown/Reboot --> <!-- Row 5: Shutdown/Reboot -->
@@ -64,15 +65,50 @@
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions> </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"/> 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"/> 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"/> Padding="5" Margin="0,5,0,0"/>
<!-- Remote Shutdown Options -->
</Grid> </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> </StackPanel>
</Window> </Window>
Binary file not shown.
Binary file not shown.