Ping requests can now be started and stopped on demand.
This commit is contained in:
@@ -9,7 +9,7 @@ namespace TimedShutdown
|
|||||||
public class PingWidget
|
public class PingWidget
|
||||||
{
|
{
|
||||||
IPAddress _ipAddress = IPAddress.None;
|
IPAddress _ipAddress = IPAddress.None;
|
||||||
public bool IsCancellationRequested { get; set; } = false;
|
public bool WidgetHasInitialized { get; private set; } = false;
|
||||||
|
|
||||||
BackgroundWorker contPingBackgroundWorker = new BackgroundWorker();
|
BackgroundWorker contPingBackgroundWorker = new BackgroundWorker();
|
||||||
|
|
||||||
@@ -17,10 +17,23 @@ namespace TimedShutdown
|
|||||||
{
|
{
|
||||||
_ipAddress = ipAddress;
|
_ipAddress = ipAddress;
|
||||||
|
|
||||||
|
contPingBackgroundWorker.WorkerSupportsCancellation = true;
|
||||||
contPingBackgroundWorker.DoWork += ContPingBackgroundWorkerDoWork;
|
contPingBackgroundWorker.DoWork += ContPingBackgroundWorkerDoWork;
|
||||||
|
|
||||||
|
if (contPingBackgroundWorker.IsBusy == false)
|
||||||
|
{
|
||||||
contPingBackgroundWorker.RunWorkerAsync();
|
contPingBackgroundWorker.RunWorkerAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WidgetHasInitialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopCurrentPingRequest()
|
||||||
|
{
|
||||||
|
contPingBackgroundWorker.CancelAsync();
|
||||||
|
Debug.WriteLine("Stopped ping request");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Will continuously ping the input IP address every 5 seconds,
|
/// Will continuously ping the input IP address every 5 seconds,
|
||||||
/// will stop if a cancellation request is made.
|
/// will stop if a cancellation request is made.
|
||||||
@@ -30,20 +43,22 @@ namespace TimedShutdown
|
|||||||
private void ContPingBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
|
private void ContPingBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
|
||||||
{
|
{
|
||||||
|
|
||||||
while (IsCancellationRequested == false)
|
while (contPingBackgroundWorker.CancellationPending == false)
|
||||||
{
|
{
|
||||||
Thread.Sleep(5000);
|
|
||||||
|
|
||||||
Ping pingSender = new Ping();
|
Ping pingSender = new Ping();
|
||||||
IPAddress address = _ipAddress;
|
IPAddress address = _ipAddress;
|
||||||
PingReply reply = pingSender.Send(address);
|
PingReply reply = pingSender.Send(address);
|
||||||
|
|
||||||
if (reply.Status == IPStatus.Success)
|
if (reply.Status == IPStatus.Success)
|
||||||
|
{
|
||||||
Debug.WriteLine("Ping Success!");
|
Debug.WriteLine("Ping Success!");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.WriteLine(reply.Status);
|
Debug.WriteLine(reply.Status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Thread.Sleep(3000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,12 +92,15 @@ namespace TimedShutdown.ViewModels
|
|||||||
|
|
||||||
if (value == true)
|
if (value == true)
|
||||||
{
|
{
|
||||||
pingWidget.IsCancellationRequested = false;
|
|
||||||
pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown));
|
pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pingWidget.IsCancellationRequested = true;
|
if (pingWidget.WidgetHasInitialized == true)
|
||||||
|
{
|
||||||
|
pingWidget.StopCurrentPingRequest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user