none
NetworkChange events not firing when multiple NICs enabled in a PC? RRS feed

  • Question

  • Hi All,
    Iam really upto a challenging task in windows app using vs2008 with c#. i want to first explain you in detail where iam using ISNetworkAvailable() method currently is NetworkChange.NetworkAvailabilityChanged method as the event fires when a user disconnects his laptop from ethernet cable and try to coonect using USB or Wireless which helps in recognising the events. But as the NetworkChange.NetworkAvailabilityChanged is not firing when multiple NIC's are on, which is where i stuckup. I tried with NetworkChange.NetworkAddressChanged event which fires regularly but it fires multiple times which is another problem. can we make it to fire only once?

    Please do find the code below and Can you suggest any better solution or better way to come out of this. Waiting eagerly for your reply.

    using System.Net.NetworkInformation;

    namespace NetworkTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                 NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
                NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            }
            private void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
            {
                if (e.IsAvailable)
                    MessageBox.Show("Network connected!");                
                else
                    MessageBox.Show("Network disconnected!");                
            }
            private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
            {
              if (e.IsAvailable)
                    MessageBox.Show("Network connected!");                
                else
                    MessageBox.Show("Network disconnected!");       
             }
    }

    }

    Thanks,

    Ram 

    ram

    Wednesday, July 24, 2013 5:17 AM

Answers

  • Hi coolram4u,

      The NetworkChange class raises NetworkAvailabilityChanged events when the availability of the network changes. The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface. So if you want multiple NIC's are online but the OperationalStatus property doesn't be changed to the Up value,the next step doesn't make any sense. Based on that, you may try using binding NetworkAddressChanged Event after  monitoring whether the OperationalStatus property is up or not. Its detailed descriptions you can follow this article:

      Detect Internet Network Availability

      Why you need to use NetworkAddressChanged Event you can follow this msdn blog article:

      http://blogs.msdn.com/b/ncl/archive/2009/03/17/online-offline-network-detection-with-net-framework.aspx 

      It seems that you have tried post the thread at NetworkAvailabilityChanged event does not fire.. . .Perhaps you doesn't understand why using both NetworkAvailabilityChanged and NetworkAddressChanged, so you need to use Delegate.Combine to dynampic bind this two event handle when OperationalStatus becomes Up. Hope you dive into the sample inside of Detect Internet Network Availability.


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".



    Thursday, August 1, 2013 6:01 AM
  • Hi,

    I do not know if I have misunderstood your idea, we can just listen for NetworkChange events within our services like below:

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
    
        protected override void OnStart(string[] args)
        {
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkAddressChanged);
        }
    
        protected override void OnStop()
        {
        }
    
        private void NetworkAddressChanged(object sender, EventArgs e)
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface n in adapters)
            {
                EventLog.WriteEntry("NetworkMonitor",String.Format("{0} is {1}", n.Name, n.OperationalStatus),EventLogEntryType.Warning);
            }
        }
    
    }

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, July 26, 2013 5:25 AM
    Moderator

All replies

  • Hi,

    I do not know if I have misunderstood your idea, we can just listen for NetworkChange events within our services like below:

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
    
        protected override void OnStart(string[] args)
        {
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkAddressChanged);
        }
    
        protected override void OnStop()
        {
        }
    
        private void NetworkAddressChanged(object sender, EventArgs e)
        {
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface n in adapters)
            {
                EventLog.WriteEntry("NetworkMonitor",String.Format("{0} is {1}", n.Name, n.OperationalStatus),EventLogEntryType.Warning);
            }
        }
    
    }

    Best Regards.


    Amy Peng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, July 26, 2013 5:25 AM
    Moderator
  • Hi Peng,

    Iam really happy to see a reply from you, And here is the brief about my situation mentioned below:

    I was trying to use a network change detction and was using NetworkAvailabilityChanged and NetworkAddressChanged Events in System.Net.NetworkInformation.NetworkChange class. It looks like the NetworkAvailabilityChanged event does not fire at all when multiple NIC's are enabled in a machine and NetworkAddressChanged Eventis firing multiple times, but i want to perform the operation only once... Looking into the event args and sender object they both are empty and null... 

    Has anyone have a workaround to fire it only once or handle it once. 

    Kindly do let me know if you ahve any answer for that

    Thanks,

    Ram


    ram

    Friday, July 26, 2013 5:30 AM
  • Hi coolram4u,

      The NetworkChange class raises NetworkAvailabilityChanged events when the availability of the network changes. The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface. So if you want multiple NIC's are online but the OperationalStatus property doesn't be changed to the Up value,the next step doesn't make any sense. Based on that, you may try using binding NetworkAddressChanged Event after  monitoring whether the OperationalStatus property is up or not. Its detailed descriptions you can follow this article:

      Detect Internet Network Availability

      Why you need to use NetworkAddressChanged Event you can follow this msdn blog article:

      http://blogs.msdn.com/b/ncl/archive/2009/03/17/online-offline-network-detection-with-net-framework.aspx 

      It seems that you have tried post the thread at NetworkAvailabilityChanged event does not fire.. . .Perhaps you doesn't understand why using both NetworkAvailabilityChanged and NetworkAddressChanged, so you need to use Delegate.Combine to dynampic bind this two event handle when OperationalStatus becomes Up. Hope you dive into the sample inside of Detect Internet Network Availability.


    orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".



    Thursday, August 1, 2013 6:01 AM