Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Threading;
- using System.Threading.Tasks;
- class StatusChecker
- {
- private int invokeCount;
- private int maxCount;
- public StatusChecker(int count)
- {
- invokeCount = 0;
- maxCount = count;
- }
- static List<long> listaSvakih10sec = new List<long>();
- static List<long> listaSvakih30sec = new List<long>();
- public void CheckStatus(Object stateInfo)
- {
- Ping pingSender = new Ping();
- AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
- var latency = pingSender.Send("8.8.8.8").RoundtripTime;
- listaSvakih10sec.Add(latency);
- Console.WriteLine("Ping at " + DateTime.Now.ToString() + " - Success! RTT: {0}", latency);
- }
- public void CalculateStatus(Object stateInfo)
- {
- AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
- List<long> test = new List<long>();
- test = listaSvakih10sec.Skip(Math.Max(0, listaSvakih10sec.Count() - 6)).ToList();
- long avg = test.Sum() / 6;
- if (avg != 0) Console.WriteLine("Average ping for last minute (trigger every 30 seconds): " + avg + " ms");
- }
- }
- class TimerExample
- {
- static void Main()
- {
- var worker1 = new AutoResetEvent(false);
- var worker2 = new AutoResetEvent(false);
- var statusChecker1 = new StatusChecker(60);
- var statusChecker2 = new StatusChecker(60);
- var timesZa10Sec = new Timer(statusChecker1.CheckStatus, worker1, 1000, 10000);
- var timerZa30Sec = new Timer(statusChecker2.CalculateStatus, worker2, 1000, 30000);
- worker1.WaitOne();
- timesZa10Sec.Dispose();
- worker2.WaitOne();
- timerZa30Sec.Dispose();
- Console.WriteLine("\nDestroying timer.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement