Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a rather large WMI application and this is the one feature that I could never get to work well with WMI. Sorry to disagree with Ted but .NET WMI support is terrible, particularly with dealing with exceptions properly and when dealing with pre-Vista operating systems.</p> <p>Two caveats when dealing with these events. First, they only work if subscribed on the main thread. However, since your log file gets created then you appear to have verified that the event is being triggered. The second problem is in the assumption your code makes about the current state of the network. </p> <p>From the look of your code you are starting with Proxy = 0, so no proxy. Then when you receive a network change event you toggle the proxy. Consider what happens when your service starts and the network is "down" already, which might very well happen during bootup depending on when your service starts in relation to the network stack initialization. You will be setting "no proxy" when the NIC is down, then setting "Proxy" when the NIC is up. This seems to be inverted to what you were trying to accomplish.</p> <p>The fix is simple, in the event you need to check the state of the network to know why the change event was fired. So, in your NetworkAvailabilityChanged event you are going to want to check NetworkInterface.GetIsNetworkAvailable() which returns true as long as there is one network interface that isn't loopback or ms-tunnel.</p> <p>For the NetworkAddressChanged event presumably you are going to want to check the address to see if it's valid or not to determine if your proxy needs to be invoked. You can do this by grabbing the </p> <p>UnicastIPAddressInformationCollection like this... NetworkInterface[] niList = NetworkInterface.GetAllNetworkInterfaces();</p> <pre><code> foreach (NetworkInterface ni in niList) { switch (ni.NetworkInterfaceType) { case NetworkInterfaceType.Ethernet: // 10baseT case NetworkInterfaceType.FastEthernetT: // 100baseT case NetworkInterfaceType.GigabitEthernet: GatewayIPAddressInformationCollection gwIPColl = ni.GetIPProperties().GatewayAddresses; UnicastIPAddressInformation uniIPInfo = null; UnicastIPAddressInformationCollection IPcoll = ni.GetIPProperties().UnicastAddresses; if (IPcoll.Count &lt;= 0) { LogFile.LogMe("No valid unicast IP address"); broken = true; break; // Cannot continue if we don't have an IP in the colletion } </code></pre> <p>. . .</p> <p>or something similar depending on how complicated your want to make it. When I check for network address validity I check the Unicast address, Netmask (.IPv4Mask) and the defined gateway from GatewayIPAddressInformationCollection (.GetIPProperties().GatewayAddresses)</p> <p>Hopefully that helps you out a little bit.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload