comparison ServerMonitor/Objects/Checks/PingCheck.cs @ 17:68d7834dc28e

More comments.
author Brad Greco <brad@bgreco.net>
date Sat, 25 May 2019 15:14:26 -0400
parents 453ecc1ed9ea
children
comparison
equal deleted inserted replaced
16:7626b099aefd 17:68d7834dc28e
1 using System; 1 using System.ComponentModel;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Linq;
5 using System.Net.NetworkInformation; 2 using System.Net.NetworkInformation;
6 using System.Text;
7 using System.Threading; 3 using System.Threading;
8 using System.Threading.Tasks; 4 using System.Threading.Tasks;
9 5
10 namespace ServerMonitorApp 6 namespace ServerMonitorApp
11 { 7 {
8 /// <summary>Checks that a server responds to ping.</summary>
12 [DisplayName("Ping check"), Description("Check if the server responds to a ping request"), DisplayWeight(0)] 9 [DisplayName("Ping check"), Description("Check if the server responds to a ping request"), DisplayWeight(0)]
13 public class PingCheck : Check 10 public class PingCheck : Check
14 { 11 {
12 /// <summary>Sends a ping and waits for a response.</summary>
15 protected async override Task<CheckResult> ExecuteCheckAsync(CancellationToken token) 13 protected async override Task<CheckResult> ExecuteCheckAsync(CancellationToken token)
16 { 14 {
17 using (Ping ping = new Ping()) 15 using (Ping ping = new Ping())
18 { 16 {
19 try 17 try
27 catch (PingException e) 25 catch (PingException e)
28 { 26 {
29 return Fail(e); 27 return Fail(e);
30 } 28 }
31 } 29 }
32
33 // Cancellable version that doesn't actulaly work
34 // might be useful as a TaskCompletionSource example though
35 //
36 //TaskCompletionSource<CheckResult> tcs = new TaskCompletionSource<CheckResult
37 //
38 //using (Ping ping = new Ping())
39 //{
40 // token.Register(ping.SendAsyncCancel);
41 // ping.PingCompleted += (sender, e) =>
42 // {
43 // if (e.Error != null)
44 // tcs.SetResult(Fail("Ping failed: " + e.Error.GetBaseException().Message));
45 // else if (e.Reply.Status != IPStatus.Success)
46 // tcs.SetResult(Fail("Ping failed: " + e.Reply.Status.ToString()));
47 // else
48 // tcs.SetResult(Pass("Ping completed in " + e.Reply.RoundtripTime + "ms"));
49 // };
50 // ping.SendAsync(Server.Host, Timeout, null);
51 //}
52
53 //return tcs.Task;
54 } 30 }
55 } 31 }
56 } 32 }