コード例 #1
0
        public async Task <PingResults> StartPing(CancellationToken cancellationToken)
        {
            //Send first ping
            PingReply reply;

            try
            {
                reply = await _pinger.SendPingAsync(_address);
            }
            catch (PingException e)
            {
                return(PingResults.BuildFailedResults(e.Message));
            }

            var currentResults = new PingResults(reply);

            OnPingReceived(reply, currentResults);

            for (var i = 1; i < _count; i++)
            {
                await Task.Delay(_delay, cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return(currentResults);
                }
                try
                {
                    reply = await _pinger.SendPingAsync(_address);
                }
                catch (PingException e)
                {
                    currentResults.MarkFailed(e.Message);
                    return(currentResults);
                }

                currentResults.AddResponse(reply);
                OnPingReceived(reply, currentResults);
            }

            return(currentResults);
        }
コード例 #2
0
        protected virtual void OnPingReceived(PingReply reply, PingResults results)
        {
            var handler = PingReceived;

            handler?.Invoke(this, new PingEventArgs(reply, results));
        }
コード例 #3
0
 public PingEventArgs(PingReply reply, PingResults results)
 {
     PingReply   = reply;
     PingResults = results;
 }