0

So basically my listview keeps appending messages which are inserted. I use the following code which always makes the listview scroll to the bottom. Please note, i have many threads in my application so i need to update the UI using the following way

this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
        {
            this.listviewMessages.ScrollIntoView(this.listviewMessages.Items[listviewMessages.Items.Count - 1]);
        }));

Now, i have a eventhandler which raises a event whenever internet connection is lost

NetworkStatus.AvailabilityChanged += new NetworkStatusChangedHandler(this.NetworkChange_NetworkAvailabilityChanged);

When this event is called my scroll of the listview goes to the top despite calling

this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
            {
                this.listviewMessages.ScrollIntoView(this.listviewMessages.Items[listviewMessages.Items.Count - 1]);
            }));

This is the code which is called to check if internet connectivity is present or not

if (NetworkInterface.GetIsNetworkAvailable())
            {
                // however, this will include all adapters
                NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface face in interfaces)
                {
                    // filter so we see only Internet adapters
                    if (face.OperationalStatus == OperationalStatus.Up)
                    {
                        if ((face.NetworkInterfaceType != NetworkInterfaceType.Tunnel) &&
                            (face.NetworkInterfaceType != NetworkInterfaceType.Loopback))
                        {
                            IPv4InterfaceStatistics statistics = face.GetIPv4Statistics();

                            // all testing seems to prove that once an interface comes online
                            // it has already accrued statistics for both received and sent...

                            if ((statistics.BytesReceived > 0) &&
                                (statistics.BytesSent > 0))
                            {
                                return true;
                            }
                        }
                    }
                }
            }

The items count of the listview is also correct. Could someone please help me on this. What am i doing wrong?

  • so, what happens inside `NetworkChange_NetworkAvailabilityChanged`? – grek40 Jan 06 '17 at 10:43
  • In NetworkChange_NetworkAvailabilityChanged, i basically created another class which would report internet availability because my application does a few downloading. So if internet connection is lost i do a few retries to check internet connection. – Aman Mishra Jan 09 '17 at 04:32
  • Do you add items in the UI Thread or somewhere else? Maybe this question (http://stackoverflow.com/questions/2091988/how-do-i-update-an-observablecollection-via-a-worker-thread read the 2 top answers) is related. – grek40 Jan 09 '17 at 08:07
  • I am adding items in the UI thread and scrolling to the last item via this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { this.listviewMessages.ScrollIntoView(this.listviewMessages.Items[listviewMessages.Items.Count - 1]); })); This works everywhere except when this eventhandler is raised. – Aman Mishra Jan 09 '17 at 08:58
  • Yes, you've shown the dispatcher call code enough. But you are having a very specific problem and you are not giving away any of your other code. If you don't provide the necessary information to reproduce the problem, then its hard to come up with a solution. – grek40 Jan 09 '17 at 09:05
  • I have edited my question and now shown the code which is used to get if internet is available or not. Your suggestion would be greatly appreaciated sir – Aman Mishra Jan 11 '17 at 04:40
  • What do you actually do when internet is available / not available? Why is there even a need to adjust the scrolling, I'd expect that you modify your collection items based on the internet availability, but that's not in the code? – grek40 Jan 11 '17 at 06:54

0 Answers0