Advertisement
moriarty41

Task1-1

Feb 2nd, 2021
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.NetworkInformation;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. class StatusChecker
  9. {
  10.     private int invokeCount;
  11.     private int maxCount;
  12.  
  13.     public StatusChecker(int count)
  14.     {
  15.         invokeCount = 0;
  16.         maxCount = count;
  17.     }
  18.  
  19.     static List<long> listaSvakih10sec = new List<long>();
  20.     static List<long> listaSvakih30sec = new List<long>();
  21.  
  22.     public void CheckStatus(Object stateInfo)
  23.     {
  24.         Ping pingSender = new Ping();
  25.  
  26.         AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
  27.  
  28.         var latency = pingSender.Send("8.8.8.8").RoundtripTime;
  29.  
  30.         listaSvakih10sec.Add(latency);
  31.  
  32.         Console.WriteLine("Ping at " + DateTime.Now.ToString() + " - Success! RTT: {0}", latency);
  33.     }
  34.  
  35.     public void CalculateStatus(Object stateInfo)
  36.     {
  37.         AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
  38.  
  39.         List<long> test = new List<long>();
  40.         test = listaSvakih10sec.Skip(Math.Max(0, listaSvakih10sec.Count() - 6)).ToList();
  41.         long avg = test.Sum() / 6;
  42.         if (avg != 0) Console.WriteLine("Average ping for last minute (trigger every 30 seconds): " + avg + " ms");
  43.     }
  44. }
  45. class TimerExample
  46. {
  47.     static void Main()
  48.     {
  49.         var worker1 = new AutoResetEvent(false);
  50.         var worker2 = new AutoResetEvent(false);
  51.  
  52.         var statusChecker1 = new StatusChecker(60);
  53.         var statusChecker2 = new StatusChecker(60);
  54.  
  55.         var timesZa10Sec = new Timer(statusChecker1.CheckStatus, worker1, 1000, 10000);
  56.         var timerZa30Sec = new Timer(statusChecker2.CalculateStatus, worker2, 1000, 30000);
  57.  
  58.         worker1.WaitOne();
  59.         timesZa10Sec.Dispose();
  60.  
  61.         worker2.WaitOne();
  62.         timerZa30Sec.Dispose();
  63.  
  64.         Console.WriteLine("\nDestroying timer.");
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement