Sunday, November 10, 2013

Collection Synchronization in WPF

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.

61 comments:

  1. This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.

    digital marketing company in india

    ReplyDelete
  2. This blog having the details of Processes running. The way of running is explained clearly. The content quality is really great. The full document is entirely amazing. Thank you very much for this blog.
    SEO Company in India
    Digital Marketing Company in India

    ReplyDelete
  3. The way of procedd running is explained nicely. thank you.
    .net Training in velachery

    ReplyDelete
  4. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  5. 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
    python training in chennai
    python training in chennai
    python training in bangalore

    ReplyDelete
  6. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  7. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  8. 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.

    Advanced 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

    ReplyDelete
  9. 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.
    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs Training in chennai

    angularjs-Training in pune

    angularjs-Training in chennai

    ReplyDelete
  10. This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot. 
    Selenium training in Chennai

    Selenium training in Bangalore

    ReplyDelete
  11. 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.
    Devops Training in Bangalore
    Best Devops Training in pune

    ReplyDelete
  12. 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
    Data science course in bangalore

    ReplyDelete
  13. After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
    Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in




    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  14. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete

  15. Such 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


    ReplyDelete
  16. Nice Post! Thank you for sharing knowledge, it was very good post to update my knowledge and improve my skills. keep blogging.
    Java Training in Electronic City

    ReplyDelete
  17. Quickbooks enterprise support Solve QuickBooks Enterprise problems using the QuickBooks Enterprise support team. Contact our Quickbooks support team at +1 (833) 400-1001 and contact your certified QuickBooks specialist for assistance.

    ReplyDelete
  18. 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.

    ReplyDelete
  19. Quickbooks enterprise support Phone number
    Get 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.

    ReplyDelete

  20. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful .Manual Testing Training in Bangalore

    ReplyDelete
  21. I have read your blog its very attractive and impressive. I like it your blog. SELENIUM training in bangalor

    ReplyDelete
  22. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge.DOTNET training in bangalore


    ReplyDelete
  23. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great post.Microsoft Dynamics CRM Training in Bangalore

    ReplyDelete
  24. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.windows azure cloud computing training in bangalore

    ReplyDelete
  25. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Became 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.

    ReplyDelete
  26. wonderful thanks for sharing an amazing idea. keep it...

    Learn 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 .

    ReplyDelete
  27. Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
    CRS Info Solutions Salesforce training for beginners   

    ReplyDelete
  28. I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.
    I have found this Salesforce training in India worth joining course. Try this Salesforce training in Hyderabad with job assistance. Join Salesforce training institutes in ameerpet with certification. Enroll for Salesforce online training in hyderabad with hands on course.  

    ReplyDelete
  29. Myself so glad to establish your blog entry since it's actually quite instructive. If it's not too much trouble continue composing this sort of web journals and I normally visit this blog. Examine my administrations.
    Go through these Salesforce Lightning Features course. Found this Salesforce CRM Using Apex And Visualforce Training worth joining. Enroll for SalesForce CRM Integration Training Program and practice well. 

    ReplyDelete
  30. Nice post. Useful information. This is always happy to know about Salesforce training in Gurgaon whose certification program helps many to get certificed. I strongly urge you to join now in Salesforce Training in Hyderabad | Course Price | Training institute and enroll for free demo at Salesforce Training in Bangalore | Course Certification Cost and Job Placement Assistance . I have gone through this Salesforce Certification Training. Why done you enroll for a free demo at Salesforce training institutes in Chennai [updated] | Course Price

    ReplyDelete
  31. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    data science training in guduvanchery

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete
  33. Great Article
    Cloud Computing Projects


    Networking Projects

    Final Year Projects for CSE


    JavaScript Training in Chennai

    JavaScript Training in Chennai

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete
  34. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    oracle training in chennai

    oracle training in tambaram

    oracle dba training in chennai

    oracle dba training in tambaram

    ccna training in chennai

    ccna training in tambaram

    seo training in chennai

    seo training in tambaram

    ReplyDelete
  35. 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.
    360DigiTMG

    ReplyDelete
  36. I need to thank you for the fantastic blog and found new information in your site.
    Data Analytics Certification Training

    ReplyDelete
  37. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    360DigiTMG Data Science Course In Pune
    360DigiTMG Data Science Training In Pune

    Thank you..

    ReplyDelete
  38. Sometime few such blogs become very helpful while getting relevant and new information related to your targeted area. As I found this blog and appreciate the information delivered to my database.
    Php projects with source code
    Online examination system in php
    Student management system in php
    Php projects for students
    Free source code for academic
    Academic projects provider in nashik
    Academic project free download

    ReplyDelete
  39. 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.

    ReplyDelete
  40. Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

    Data Science Course In Hyderabad
    Data Science Training In Hyderabad
    Best Data Science Course In Hyderabad

    Thank you..

    ReplyDelete