Wpf listview binding observablecollection not updating

NET 4.5 added asynchronous language features for C# and VB.For the most part, this has made it much easier to improve a user interface’s responsiveness—you can use asynchronous APIs to perform potentially slow work in a way that will not cause your user interface to freeze, and yet you can use simple programming techniques that look very similar to those used in single-threaded code. There are some kinds of slow, IO-oriented work (i.e., the kind of work that often benefits most from asynchrony) where a simple application of these techniques won’t help as much as you might hope.But we’ve paid a heavy price for it: it now takes over three times as long for the operation to finish.

We’re loading a moderately large quantity of data from disk, so at least some of the work will be IO bound, and that’s where async usually shines.This implies that there’s a small overhead for using asynchronous code—it takes 0.2 seconds longer than the synchronous version, a barely perceptible increase when you’ve already been waiting 2.6 seconds.And in exchange for that, the UI remains responsive while we’re waiting for the work to complete.Comparing this with the synchronous code’s 2.6 seconds is a little misleading.For one thing, the synchronous version takes more like 4 seconds the very first time it runs.However, when we run the asynchronous version, the method does not complete immediately.(Even though the files are in the cache, the OS still has to copy the data into our process, so although file reads are very quick, they will not all complete synchronously.) And each time our method relinquishes the thread, WPF’s data binding takes that opportunity to process all the work that it had deferred.We can try the same trick of holding back the data source until we’re ready with the synchronous case: collection caused WPF to attach a change notification handler.And because we did this before populating the list, this handler ran for each of the 180,000 entries added to that list.So we’re being slightly unfair on the asynchronous version by removing its main opportunity to show an advantage.However, since the IO only slows things down by about 1.4 seconds when things aren’t in the cache, it’s hard to see how the asynchronous code might make up the deficit even in more favourable conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

One thought on “wpf listview binding observablecollection not updating”