C3204
2025-12-31 eb503eb0b578fb7e4219cb7e913d7c268b641d31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LB_SmartVisionCommon
{
    public class NetworkMonitor
    {
        private System.Timers.Timer _timer;
 
        public event Action<bool> NetworkStatusChanged;
 
        public void StartMonitoring(int intervalSeconds = 30, string ip = "www.google.com")
        {
            _timer = new System.Timers.Timer(intervalSeconds * 1000);
            _timer.Elapsed += async (s, e) => await CheckNetworkAsync(ip);
            _timer.Start();
        }
 
        public void StopMonitoring()
        {
            _timer?.Stop();
            _timer?.Dispose();
        }
 
        private async Task CheckNetworkAsync(string ip = "www.google.com")
        {
            var isConnected = await NetworkTester.TestConnectionAsync(ip);
            NetworkStatusChanged?.Invoke(isConnected);
        }
    }
}