Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:31:22+00:00 2026-06-17T11:31:22+00:00

See bottom of post for my pseudo solution. Once again I’m completely and utterly

  • 0

See bottom of post for my pseudo solution.

Once again I’m completely and utterly stuck on this. I’ve burned hours trying to understand – and yes I can get a single collectionviewsource to work beautifully with nothing about threading on the code behind.

Imagine my shock when I found merely adding two collectionviewsources on the page causes threading issues. I’ve spent a few hours last night reading Async in C#5 and the MSDN stuff however I get into work today and I can’t decipher how to make this happen.

The code below is the last attempt I’ve made before whining for help as I’ve burnt, possibly, a bit too much work time on attempting to understand how to do this. I understand that I need one collectionviewsource to complete before starting the other, so I tried Await Task.ContinueWith etc to try and chain one after the other.

Lining up both sets of tasks in the threads correctly seems to be quite tricky, or I’m still misunderstanding something fundemental.

If anyone can advise how they would asynchronously populate a few controls on a WPF UI I would be very grateful.

The application itself is a throwaway application, linked to an Access database that I’m using to try and become fluent enough in threading to implement it in our proper code base. I’m long way off that!

Updated with more complete code samples and the adjustments made according to answers:

Private Async Sub MainWindowLoaded(sender As Object, e As RoutedEventArgs) Handles MyBase.Loaded
InitializeComponent()

Dim personSetViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("personSetViewSource"), System.Windows.Data.CollectionViewSource)
Dim contactSetViewSource As System.Windows.Data.CollectionViewSource = CType(Me.FindResource("contactSetViewSource"), System.Windows.Data.CollectionViewSource)

Dim personList = Await Task.Run(Function() personSet.personList)
personSetViewSource.Source = personList

Dim contactList = Await Task.Run(Function() contactSet.contactList)
contactSetViewSource.Source = contactList

End Sub`

The ObservableCollectionEx class:

public class ObservableCollectionEx<T> : ObservableCollection<T>
{
public override event NotifyCollectionChangedEventHandler CollectionChanged;

protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
    using (BlockReentrancy())
    {
        NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged;
        if (collectionChanged != null)
            foreach (NotifyCollectionChangedEventHandler nh in collectionChanged.GetInvocationList())
            {
                DispatcherObject dispObj = nh.Target as DispatcherObject;
                if (dispObj != null)
                {
                    Dispatcher dispatcher = dispObj.Dispatcher;
                    if (dispatcher != null && !dispatcher.CheckAccess())
                    {
                        NotifyCollectionChangedEventHandler nh1 = nh;
                        dispatcher.BeginInvoke(
                            (Action) (() => nh1.Invoke(this,
                                                       new NotifyCollectionChangedEventArgs(
                                                           NotifyCollectionChangedAction.Reset))),
                            DispatcherPriority.DataBind);
                        continue;
                    }
                }
                nh.Invoke(this, e);
            }
    }
}
}

Please note, I can’t translate this class to VB due to requiring an Event Override.
Another variation I’ve tried, but falls foul of thread ownership again. The two collectionviews thing isn’t yielding to a solution: I don’t know if it’s because the underlying collection isn’t good for it or whether in reality it wasn’t meant to work that way. I get close but no cigar.

 Dim CarePlanList = Task.Run(Function() CarePlanSet.CarePlanList)
    Dim rcpdList = Task.Run(Function() rcpdSet.rcpdList)

    Dim tasks() As Task = {CarePlanList, rcpdList}
    Dim t = New TaskFactory
    Await t.ContinueWhenAll(tasks, Sub()
                                       carePlanSetViewSource.Source = CarePlanList
                                       rcpdSetViewSource.Source = rcpdList
                                   End Sub)

I found a way to do it, based on a combination of feedback and research this morning. Building the two collectionviews asynchronously itself is somewhat impractical given the STAThread model of WPF. However, merely ensuring one HAS completed and shifting some of the async out of one entity class has made this plausible.

Instead I fire off the first task, who’s underlying class does build it’s data with its own Async method. Then test to see if it has completed before allowing the second collectionview to be fired off. This way I don’t need to worry about context or dispatcherobjects. The second collection does not use any async.

    Dim personList = Task(Of List(Of person)).Run(Function() personSet.personList)
    Dim contactList = Task(Of ObservableCollectionEx(Of contact)).Run(Function() contactSet.contactList)

    contactSetViewSource.Source = contactList.Result
    If contactList.IsCompleted Then personSetViewSource.Source = personList.Result

This is an experimental project for concept research really. As it happens, the idea I’d want two such lists built this way isn’t as useful as all that but I do see where being able to compose a data heavy interface asynchronously could be handy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:31:23+00:00Added an answer on June 17, 2026 at 11:31 am

    Your two code samples each have issues that jump out right away.

    In the first you are awaiting task1, I assume with more code following, but all task1 is doing is starting what is basically a fire and forget operation back to the UI thread (Dispatcher.BeginInvoke), therefore not really producing anything asynchronous to await.

    In the second, the primary issue seems to be that you are doing a lot of setup of Task instances and chaining them with continuations but never starting the action2 Task, which appears to be the root of the whole chain, hence getting no activity at all. This is similar to what you get with a BackgroundWorker that never has RunWorkerAsync called.

    To get this working properly and avoid making your head spin any more I would suggest starting by writing this whole block without any async and verifying that everything loads as expected, but with the UI lockup you want to avoid. Async/Await is designed to be added into code like that with minimal structural changes. Using Task.Run along with async and await you can then make the code asynchronous.

    Here’s some pseudocode for the basic pattern, without async to start:

    PersonSetList = LoadData1()
    CVS1.Source = PersonSetList
    ContactList = LoadData2()
    CVS2.Source = ContactList
    

    and now adding async:

    PersonSetList = await Task.Run(LoadData1())
    CVS1.Source = PersonSetList
    ContactList = await Task.Run(LoadData2())
    CVS2.Source = ContactList
    

    What this will now do is start a task to load the person data and immediately return from your WindowLoaded method, allow the UI to continue rendering. When that data is loaded it will continue to the next line on the original thread and push the data into the UI (which may itself slow down the UI while rendering). After that it will do the same for the Contact data. Notice that Dispatcher isn’t needed explicitly because await is returning back to the UI thread for you to complete its continuation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update Solution Found See Bottom of post if interested Seems simple enough and for
Problem Solved - See bottom for solution notes I'm trying to build a simple
Intro: EDIT: See solution at the bottom of this question (c++) I have a
This post has been 'somewhat' addressed (see links at bottom of post) but not
Scroll down to the bottom of this post to see a work around /
Note: See the bottom of this post for an explanation for why this wasn't
SEE BOTTOM OF THIS POST FOR UPDATE ON THIS PLEASE. I have the below
http://madisonlane.businesscatalyst.com I'm trying to get the div#sign-post to sit above the div#bottom. This works
Got solution - See bottom of the post Just want to know if there
(Note: This post has been edited to show specific use case. See bottom.) I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.