I like the MVVM pattern in WPF. No doubt, it’s
a perfect way to separate UI from logic. But you also get some other features out
of the box when you apply this pattern. One of my favorites is binding
collections to the UI and manipulating them with the use of a background
thread. If you’re doing it right, you get a responsive UI while performing
heavy background work without any additional effort required synchronizing the
data to the view. WPF is just doing it for you.
Ok. That’s not 100% of the truth and of course
it’s not that easy as it sounds. But there is not much to do. The data is
coming from a background thread and it should be displayed in the UI thread.
You know what that means? Correct. Someone has to dispatch the data to the UI
thread. Luckily there’s a class called Dispatcher in WPF. Even better, you can
use the Dispatcher anywhere in your WPF application because it provides a
static property (Current) that gives you access to the current Dispatcher
instance of the application. On the instance object you have the possibility to
invoke any method on the UI thread you like, either synchronous or asynchronous.
That was the preferred way until WPF 4.5. The problem was that it was not
working in every case. Have you ever tried to modify an ObersableCollection
from a background thread while it is bound to a ListView? The invoke methods on
the Dispatcher won’t help you. With WPF 4.5 there’s a better and more comfortable
approach to that problem: The EnableCollectionSynchronization method.
The EnableCollectionSynchronization method is a
static method on the BindingOperations class in the System.Windows.Data
namespace. The method has two overloads for controlling the access to the
underlying collection from several threads (just have a look at http://msdn.microsoft.com/en-us/library/system.windows.data.bindingoperations.enablecollectionsynchronization(v=vs.110).aspx).
So, with this method you may just write something like the following in your view
model
BindingOperations.EnableCollectionSynchronization(ListItems,
lockObject);
where ListItems is your observable collection
and lockObject is some object that will be used by the infrastructure to synchronize
the access to the collection from different threads. The magic happens behind
the scenes. The method is just a wrapper to some internal functionality
enabling the underlying CollectionView to allow cross thread changes. If you
want to know some more about WPF internals on handling views and collections
you’re invited to read the next parts of the article, otherwise, if you just
want to perform cross thread changes to collections bound to UI elements not
worrying about how it works, then all relevant information has just been said.
WPF View Management
While using the new EnableCollectionSynchronization
method I was wondering what happened behind the scenes when I use that method.
In order to cure my curiosity I had a look into the framework and its internal
classes. The key requirement of the WPF team was to allow the binding of one
collection to many views without having strong references. Additionally the
views should be able to sort, filter and navigate the collection without
affecting other views or even the source collection itself. Because of those
requirements the collection cannot manage its associated views and view
management has to happen outside of the collection view relationship. That’s
why there is a global ViewManager class that exists per application. This is an
internal class with a static Current property. The view manager is just a
dictionary that maps all bound collections to corresponding CollectionRecord
objects. The collection record is an
internal utility class that keeps a weak reference to a view table and also has
its own SynchronizationInfo. The ViewTable class is again a dictionary that
manages CollectionViewSources and ViewRecords. Again, the view record acts as a
utility class and encapsulates the access to the underlying CollectionView that
is used by WPF for realizing sorting, filtering, grouping and navigation functionalities
of collections on the UI. The collection view source is the XAML proxy of the
collection view. The following picture shows the relationships between the
mentioned classes.
When you call the EnableCollectionSynchronization
method, the call is handled by the RegisterCollectionSynchronizationCallback
method of the view manager, which in turn passes it to every collection view
that belongs to the given collection. The CollectionView class contains an
internal method SetAllowsCrossThreadChanges (see the following listing) which
just sets a flag in the collection view whether to allow or deny cross thread
operations on the underlying collection.
internal void SetAllowsCrossThreadChanges(bool value){
bool flag = this.CheckFlag(
CollectionView.CollectionViewFlags.AllowsCrossThreadChanges);
if (flag == value)
{
return;
}
this.SetFlag(
CollectionView.CollectionViewFlags.AllowsCrossThreadChanges, value);
this.OnAllowsCrossThreadChangesChanged();
}
This flag is evaluated on binding operations (in
our case changing the observable collection). The synchronization info on the
collection record is used to synchronize the access the collection. This is
where the two overloads of the EnableCollectionSynchronization come into play.
The first one takes a lock object that is now used for synchronization. The
second overload takes a callback that will be called during synchronization to
handle the access among several threads. This provides the possibility for the
client to manage the synchronization itself.
The way of procedd running is explained nicely. thank you.
ReplyDelete.net Training in velachery
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
ReplyDeletepython training in chennai
python training in chennai
python training in bangalore
Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
ReplyDeleteJava training in Bangalore | Java training in Marathahalli
Java training in Bangalore | Java training in Btm layout
Java training in Bangalore |Java training in Rajaji nagar
Java training in Bangalore | Java training in Kalyan nagar
Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
ReplyDeleteAdvanced AWS Training in Marathahalli |No.1 AWS Training in Marathahalli
Best AWS Amazon Web Services Training Institute in Chennai | No.1 AWS Training Institutes for Solution Architect in Chennai | Advanced AWS Certification Training in Chennai
Great information..I really love this blog
ReplyDeletejava training in Marathahalli
spring training in Marathahalli
java training institute in Marathahalli
spring and hibernate training in Marathahalli
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleteangularjs-Training in tambaram
angularjs-Training in sholinganallur
angularjs-Training in velachery
angularjs Training in chennai
angularjs-Training in pune
angularjs-Training in chennai
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteDevops Training in Bangalore
Best Devops Training in pune
You’ve written a really great article here. Your writing style makes this material easy to understand.. I agree with some of the many points you have made. Thank you for this is real thought-provoking content
ReplyDeleteData science course in bangalore
ReplyDeleteThanks for the warn and great post dear!
Deer Hunting Tips Camping Trips Guide DEER HUNTING TIPS travel touring tips
Wanderfull post im waiting for your next post
ReplyDeleteStill Hunting Method
Hunting psych tips Survival Tips Travel Touring Tips
ReplyDeleteSuch a nice & useful post. Really happy to see such post. I have come to know about many new ideas. I will try my best to implement some of them. Thanks.travel trekking tips
see the link Tent Camping 101 Exploring Smithriver
Nice Post! Thank you for sharing knowledge, it was very good post to update my knowledge and improve my skills. keep blogging.
ReplyDeleteJava Training in Electronic City
Quickbooks enterprise support number +1 (833) 400-1001 is available to solve QuickBooks Enterprise issues through QuickBooks Enterprise support. Contact our Quickbooks support team at +1 (833) 400-1001 and contact your certified QuickBooks specialist for assistance.
ReplyDeleteQuickbooks enterprise support Phone number
ReplyDeleteGet support 24 hours a day for Enterprise Quickbooks by calling a QuickBooks Enterprise phone number. We are ready to solve the QuickBooks Enterprise problems through the QuickBooks Enterprise support group. Contact our Quickbooks support team at +1 (833) 400-1001 and contact your certified QuickBooks specialist for assistance.
I have read your blog its very attractive and impressive. I like it your blog. SELENIUM training in bangalor
ReplyDeleteReally i appreciate the effort you made to share the knowledge. The topic here i found was really effective...
ReplyDeleteBecame an Expert In Google Cloud Platform Training in Bangalore! Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.
wonderful thanks for sharing an amazing idea. keep it...
ReplyDeleteLearn SAP from the Industry Experts we bridge the gap between the need of the industry. eTechno Soft Solutions provide the Best IT Training in Bangalore .
Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.
ReplyDeleteaws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
Thanks for this wonderful blog it is really informative to all.keep update more information about this
ReplyDeleteaws Training in Bangalore
python Training in Bangalore
hadoop Training in Bangalore
angular js Training in Bangalore
bigdata analytics Training in Bangalore
python Training in Bangalore
aws Training in Bangalore
Your doing great job Thanks for sharing this blog
ReplyDeleteAngularJS Training in Chennai | AngularJS Training in Anna Nagar | AngularJS Training in OMR | AngularJS Training in Porur | AngularJS Training in Tambaram | AngularJS Training in Velachery
This article is very nice content. Thank for that your blog.
ReplyDeletePython Training in Chennai
Python Training in Training
Python Training in Bangalore
Python Hyderabad
Python Training in Coimbatore
This comment has been removed by the author.
ReplyDeleteFirst i got a great blog .I will be interested in more similar topics. i see you got really very useful topics,
ReplyDeletei will be always checking your blog thanks
web designing training in chennai
web designing training in porur
digital marketing training in chennai
digital marketing training in porur
rpa training in chennai
rpa training in porur
tally training in chennai
tally training in porur
his is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche..
ReplyDeletehadoop training in chennai
hadoop training in omr
salesforce training in chennai
salesforce training in omr
c and c plus plus course in chennai
c and c plus plus course in omr
machine learning training in chennai
machine learning training in omr
Blog was amazing with useful information thanks for sharing.
ReplyDeleteData Science Training in Hyderabad 360DigiTMG
I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
ReplyDelete360DigiTMG
I need to thank you for the fantastic blog and found new information in your site.
ReplyDeleteData Analytics Certification Training
By purchasing a Hello Baby monitor like the Hello Nurse, you will be able to keep an eye and ear on your little one at all time. This is a very useful way for you to give yourself and your spouse a peace of mind by knowing that you're basically there for your little one at all times.
ReplyDeleteThe blog gives me a very good impression and loved it. Really amazing. Its a must read blog.! Thanks for sharing
ReplyDeleteSelenium Training in Chennai
Selenium Training in Velachery
Selenium Training in Tambaram
Selenium Training in Porur
Selenium Training in Omr
Selenium Training in Annanagar
NICE
ReplyDeleteJava training in chennai
python training in chennai
web designing and development training course in chennai
selenium training in chennai
digital-marketing seo training in chennai
Thanks for the warn and great post dear!
ReplyDeleteIELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
Spoken english classes in chennai | Communication training
Excellent content! Thanks for sharing such a useful information.
ReplyDeletebest apache spark online course
apache spark and scala online training
Even if you are armed with an abundance of knowledge in SEO, you may still need the services of a professional SEO company. Having strong knowledge of SEO is hardly sufficient in some cases. 2000 Backlink at cheapest
ReplyDelete5000 Backlink at cheapest
Boost DA upto 15+ at cheapest
Boost DA upto 25+ at cheapest
Boost DA upto 35+ at cheapest
Boost DA upto 45+ at cheapest
Even if you are doing your best to attempt to obtain a good ranking for your website, you might still wind up wondering why your website is simply not doing well.
informative article.thank you for sharing.
ReplyDeleteAngular training in Chennai
Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job!
ReplyDeleteAWS Training in Hyderabad
AWS Course in Hyderabad