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

  • Home
  • SEARCH
  • 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 4114300
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:26:17+00:00 2026-05-20T22:26:17+00:00

I have what I think is a fairly standard setup, a ListBox backed by

  • 0

I have what I think is a fairly standard setup, a ListBox backed by an ObservableCollection.

I have some work to do with the Things in the ObservableCollection which might take a significant amount of time (more than a few hundred milliseconds) so I’d like to offload that onto a Task (I could have also used BackgroundWorker here) so as to not freeze the UI.

What’s strange is that when I do CollectionViewSource.GetDefaultView(vm.Things).CurrentItem before starting the Task, everything works as expected, however if this happens during the Task then CurrentItem seems to always point to the first element in the ObservableCollection.

I’ve drawn up a complete working example.

XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <ToolBar DockPanel.Dock="Top">
            <Button Content="Click Me Sync" Click="ButtonSync_Click" />
            <Button Content="Click Me Async Good" Click="ButtonAsyncGood_Click" />
            <Button Content="Click Me Async Bad" Click="ButtonAsyncBad_Click" />
        </ToolBar>
        <TextBlock DockPanel.Dock="Bottom" Text="{Binding Path=SelectedThing.Name}" />
        <ListBox Name="listBox1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=Things}" SelectedItem="{Binding Path=SelectedThing}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </DockPanel>
</Window>

C#:

public partial class MainWindow : Window
{
    private readonly ViewModel vm;

    public MainWindow()
    {
        InitializeComponent();
        vm = new ViewModel();
        DataContext = vm;
    }

    private ICollectionView GetCollectionView()
    {
        return CollectionViewSource.GetDefaultView(vm.Things);
    }

    private Thing GetSelected()
    {
        var view = GetCollectionView();
        return view == null ? null : (Thing)view.CurrentItem;
    }

    private void NewTask(Action start, Action finish)
    {
        Task.Factory
            .StartNew(start)
            .ContinueWith(t => finish());
            //.ContinueWith(t => finish(), TaskScheduler.Current);
            //.ContinueWith(t => finish(), TaskScheduler.Default);
            //.ContinueWith(t => finish(), TaskScheduler.FromCurrentSynchronizationContext());
    }

    private void ButtonSync_Click(object sender, RoutedEventArgs e)
    {
        var thing = GetSelected();
        DoWork(thing);
        MessageBox.Show("all done");
    }

    private void ButtonAsyncGood_Click(object sender, RoutedEventArgs e)
    {
        var thing = GetSelected(); // outside new task
        NewTask(() =>
        {
            DoWork(thing);
        }, () =>
        {
            MessageBox.Show("all done");
        });
    }

    private void ButtonAsyncBad_Click(object sender, RoutedEventArgs e)
    {
        NewTask(() =>
        {
            var thing = GetSelected(); // inside new task
            DoWork(thing); // thing will ALWAYS be the first element -- why?
        }, () =>
        {
            MessageBox.Show("all done");
        });
    }

    private void DoWork(Thing thing)
    {
        Thread.Sleep(1000);
        var msg = thing == null ? "nothing selected" : thing.Name;
        MessageBox.Show(msg);
    }
}

public class ViewModel
{
    public ObservableCollection<Thing> Things { get; set; }
    public Thing SelectedThing { get; set; }

    public ViewModel()
    {
        Things = new ObservableCollection<Thing>();
        Things.Add(new Thing() { Name = "one" });
        Things.Add(new Thing() { Name = "two" });
        Things.Add(new Thing() { Name = "three" });
        Things.Add(new Thing() { Name = "four" });
    }
}

public class Thing
{
    public string Name { get; set; }
}
  • 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-05-20T22:26:18+00:00Added an answer on May 20, 2026 at 10:26 pm

    I believe CollectionViewSource.GetDefaultView is effectively thread-static – in other words, each thread will see a different view. Here’s a short test to show that:

    using System;
    using System.Windows.Data;
    using System.Threading.Tasks;
    
    internal class Test
    {
        static void Main() 
        {
            var source = "test";
            var view1 = CollectionViewSource.GetDefaultView(source);
            var view2 = CollectionViewSource.GetDefaultView(source);        
            var view3 = Task.Factory.StartNew
                (() => CollectionViewSource.GetDefaultView(source))
                .Result;
    
            Console.WriteLine(ReferenceEquals(view1, view2)); // True
            Console.WriteLine(ReferenceEquals(view1, view3)); // False
        }        
    }
    

    If you want your task to work on a particular item, I suggest you fetch that item before starting the task.

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

Sidebar

Related Questions

I have a fairly complex application which has been broken up into multiple components.
On a standard web signup form, users are required to have a unique email
I'm trying to create standard button in android with a background and some text
I have a fairly hefty project, where I am loading a few view controllers,
So i am fairly fluent with python and have used urllib2 and Cookies a
I have Visual C# 2008 Professional and have developed the first half of a
In the game of Rummikub, for those who don't know it, you have tiles
All right, let me preface this by saying: I'm not completely confident this is
I'm brand new to Joomla but after browsing around a demo site and doing
I'm hoping someone can either tell me what I'm doing wrong correct my flawed

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.