locked
NetworkInformation.NetworkStatusChanged event fired multiple times on connecting to the Internet, bug? RRS feed

  • Question

  • Hi there,

    I am trying to do some automatic loading of data from web-services when the user reconnects to the internet. I know when this happens because the NetworkInformation.NetworkStatusChanged event fires, I check to see that there is an InternetConnectionProfile, and if there is, I load the online data.

    The problem is that when (in testing) I connect my ethernet cable to go online, the event fires three (sometimes 4) times to say I am now connected to the internet. This is a problem because then my event handler runs three times, which I obviously don't want to happen.

    Neither do I really want to make a hacky way of preventing the handler from running multiple times (through looking at same shared variable or locking objects).

    Is this a bug with NetworkStatusChanged? Why does it fire multiple times? If there was some difference in the ConnectionProfile object with each event, I could probably look for that difference and only handle the event in that case, but I can't see any difference between events.

    Tuesday, July 24, 2012 3:51 PM

Answers

  • I had originally tried looking into the different object values that came through with each callback but that was hard to debug because while I was looking at the values in VS2012, I'd miss the second callback.  So I worked around the problem by just using a Boolean variable and if it was set, I would clear it and then the code would ignore the next callbacks.
    Saturday, January 26, 2013 4:40 AM

All replies

  • Hi Sam - I'll look for a bug on this and see what I can find out.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    Wednesday, July 25, 2012 2:51 PM
    Moderator
  • Thanks Matt.
    Wednesday, July 25, 2012 2:52 PM
  • Sam - we had some known issues previously, but these were fixed by RC.  Can you send me a project to review? MSMALL at Microsoft.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.


    Tuesday, July 31, 2012 3:09 PM
    Moderator
  • Was this problem ever figured out?  I'm getting the same behavior with the RTM version.  In the App.xaml.cs OnLaunched event I have the following:

    NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;

    If I start out with the app being offline, then connect to WiFi, this event fires three or four times.

    Saturday, December 1, 2012 2:46 AM
  • I'm still running into this issue as well on RTM. This issue can be reproduced in using the "Network information sample" from the samples library. To reproduce set a breakpoint inside NetworkStatusChange_Click in NetworkStatusChange.xaml.cs. The method will get called only once when the connection is turned off, but will fire twice when the connection is turned back on.
    Monday, December 17, 2012 2:04 AM
  • This was fixed in RC.  I'll see if I can repro this issue again using your instructions.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, December 17, 2012 4:27 PM
    Moderator
  • Matt - any updates on this one?  I've got a real copy of Windows 8 installed from CD and I can repro this with the sample.  In my case, when I disconnect OR connect, the callback fires twice.  I'm trying to do this and get access denied exceptions sometimes.

    try
    {
        MessageDialog dialog = new MessageDialog("You are now connected to the Internet.", "Status");
        await dialog.ShowAsync();
    }
    catch (UnauthorizedAccessException ex)
    {
        // access denied sometimes when this fires twice so trap/ignore
        // doesn't work
    }
    catch (Exception ex)
    {
        throw new Exception("Exception while handling reconnection of Internet. Exception: " + ex.Message);
    }

    Friday, January 25, 2013 2:41 AM
  • I haven't tried this but I believe that you're seeing this problem.  My thought on this is:  how can you mitigate the problem caused by this?  Let's see this page:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh700376.aspx

    Retrieving the connection state change information

    When a status change occurs for the Internet connection profile, the following event handler example provides the ConnectionProfile associated with the event and displays the connection status information, which includes the current scope, type, and cost of connectivity as defined by    NetworkConnectivityLevel NetworkTypes, and NetworkCostType.

    Now when a status change occurs for the Internet connection profile, the following example function provides the  associated ConnectionProfile.

    I don't know what your exact scenarios are, but it seems that you can figure out exactly what status your connection is in when the event fires.  If you have multiple connection events on the same connection, then it seems natural to ignore the subsequent connections.  Is there a scenario here that you are encountering that can't be ignored? 


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Proposed as answer by Lisa19 Saturday, January 26, 2013 4:40 AM
    Friday, January 25, 2013 3:55 PM
    Moderator
  • I had originally tried looking into the different object values that came through with each callback but that was hard to debug because while I was looking at the values in VS2012, I'd miss the second callback.  So I worked around the problem by just using a Boolean variable and if it was set, I would clear it and then the code would ignore the next callbacks.
    Saturday, January 26, 2013 4:40 AM