Ping requests can now be started and stopped on demand.
This commit is contained in:
@@ -9,7 +9,7 @@ namespace TimedShutdown
|
||||
public class PingWidget
|
||||
{
|
||||
IPAddress _ipAddress = IPAddress.None;
|
||||
public bool IsCancellationRequested { get; set; } = false;
|
||||
public bool WidgetHasInitialized { get; private set; } = false;
|
||||
|
||||
BackgroundWorker contPingBackgroundWorker = new BackgroundWorker();
|
||||
|
||||
@@ -17,8 +17,21 @@ namespace TimedShutdown
|
||||
{
|
||||
_ipAddress = ipAddress;
|
||||
|
||||
contPingBackgroundWorker.WorkerSupportsCancellation = true;
|
||||
contPingBackgroundWorker.DoWork += ContPingBackgroundWorkerDoWork;
|
||||
contPingBackgroundWorker.RunWorkerAsync();
|
||||
|
||||
if (contPingBackgroundWorker.IsBusy == false)
|
||||
{
|
||||
contPingBackgroundWorker.RunWorkerAsync();
|
||||
}
|
||||
|
||||
WidgetHasInitialized = true;
|
||||
}
|
||||
|
||||
public void StopCurrentPingRequest()
|
||||
{
|
||||
contPingBackgroundWorker.CancelAsync();
|
||||
Debug.WriteLine("Stopped ping request");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -30,20 +43,22 @@ namespace TimedShutdown
|
||||
private void ContPingBackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
|
||||
{
|
||||
|
||||
while (IsCancellationRequested == false)
|
||||
while (contPingBackgroundWorker.CancellationPending == 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);
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(3000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,12 +92,15 @@ namespace TimedShutdown.ViewModels
|
||||
|
||||
if (value == true)
|
||||
{
|
||||
pingWidget.IsCancellationRequested = false;
|
||||
|
||||
pingWidget.InitatePingRequest(IPAddress.Parse(RemoteShutdown));
|
||||
}
|
||||
else
|
||||
{
|
||||
pingWidget.IsCancellationRequested = true;
|
||||
if (pingWidget.WidgetHasInitialized == true)
|
||||
{
|
||||
pingWidget.StopCurrentPingRequest();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user