Exemple #1
0
 static void Main(string[] args)
 {
     var networkWatcher = new MacWatcher();
     networkWatcher.MacConnected += new EventHandler<KeyValuePair<string,string>>(networkWatcher_MacConnected);
     networkWatcher.MacDisconnected += new EventHandler<KeyValuePair<string, string>>(networkWatcher_MacDisconnected);
     networkWatcher.WatchNetwork();
     Console.ReadKey();
 }
Exemple #2
0
        //public List<string> GetLanIPs()
        //{
        //    if (macCache == null) return null;
        //    return macCache.Values.Distinct().ToList();
        //}

        /// <summary>
        /// Infinatelly loops to fire off MAC notification events. On same thread.
        /// </summary>
        public void WatchNetwork()
        {
            while (true)
            {
                var macs = new MacWatcher().GetMacsInLan();

                if (macCache == null)
                {
                    macCache = new ConcurrentDictionary <string, string>(macs);
                    if (this.FirstAddressesLoaded != null)
                    {
                        ConsoleDebug(macs);
                        FirstAddressesLoaded(this, new Dictionary <string, string>(macCache));
                    }
                }

                var newlyconnected = macs.Except(macCache);
                var disconnected   = macCache.Except(macs);

                foreach (var mac in disconnected)
                {
                    if (this.MacDisconnected != null)
                    {
                        this.MacDisconnected(this, mac);
                    }
                }

                foreach (var mac in newlyconnected)
                {
                    if (this.MacConnected != null)
                    {
                        this.MacConnected(this, mac);
                    }
                }

                macCache = new ConcurrentDictionary <string, string>(macs);

                System.Threading.Thread.Sleep(1000);
            }
        }
Exemple #3
0
        //public List<string> GetLanIPs()
        //{
        //    if (macCache == null) return null;
        //    return macCache.Values.Distinct().ToList();
        //}
        /// <summary>
        /// Infinatelly loops to fire off MAC notification events. On same thread.
        /// </summary>
        public void WatchNetwork()
        {
            while (true)
            {
                var macs = new MacWatcher().GetMacsInLan();

                if (macCache == null)
                {
                    macCache = new ConcurrentDictionary<string, string>(macs);
                    if (this.FirstAddressesLoaded != null)
                    {
                        ConsoleDebug(macs);
                        FirstAddressesLoaded(this, new Dictionary<string, string>(macCache));
                    }
                }

                var newlyconnected = macs.Except(macCache);
                var disconnected = macCache.Except(macs);

                foreach (var mac in disconnected)
                    if (this.MacDisconnected != null)
                        this.MacDisconnected(this, mac);

                foreach (var mac in newlyconnected)
                    if (this.MacConnected != null)
                        this.MacConnected(this, mac);

                macCache = new ConcurrentDictionary<string, string>(macs);

                System.Threading.Thread.Sleep(1000);
            }
        }