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 7177933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:49:01+00:00 2026-05-28T16:49:01+00:00

I have a thread safe observable collection replacement which I would like to write

  • 0

I have a thread safe observable collection replacement which I would like to write a unit test for. To avoid a false positive I’m trying to write a multi-threaded test that proves an object cannot be added to an ObservableCollection<> without failing so I can swap it with mine and watch it go green. I just cannot get this test to fail (using NUnit).

In no particular order I’ve so far tried:

  • Creating the collection on a different thread and updating on current
  • Creating the collection on the current thread and updating on an alternate
  • Using different threading mechanisms
    • ThreadStart
    • Dispatcher
    • BackgroundWorker
  • Using different apartment states
    • All combinations of STA and MTA on the test itself and/or one or both of the threads
  • Creating a WPF Window to hold the collection and manipulating the Dispatcher frames manually to simulate a runtime environment.

The custom collection itself is working fine in real code so this is now more of an academic exercise than anything; My threading confidence has been shaken 🙂

  • 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-28T16:49:02+00:00Added an answer on May 28, 2026 at 4:49 pm

    You’re trying to test something that is not there…

    There is no reason for this test to fail, because the ObservableCollection<T> class itself doesn’t have thread affinity. It’s not thread-safe, but it just means the behavior will be unpredictable if you use it in a multithread scenario without proper locking; there is nothing in ObservableCollection<T> that will explicitly throw an exception if you do this.

    However, the CollectionView class does have thread affinity, which is why you can’t add items to an ObservableCollection<T> from a different thread if there is a CollectionView attached to it (which happens, for instance, when you bind an ItemsControl to the collection). But it’s the CollectionView that throws an exception, not the ObservableCollection<T>…

    Take the following code:

    var list = new ObservableCollection<string>();
    // var view = CollectionViewSource.GetDefaultView(list);
    ThreadPool.QueueUserWorkItem(_ => list.Add("foo"));
    

    It executes without throwing an exception, but if you uncomment the line that creates the CollectionView, it will throw a NotSupportedException:

    This type of CollectionView does not support changes to its
    SourceCollection from a thread different from the Dispatcher thread.

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

Sidebar

Related Questions

I would like to have thread-safe read and write access to an auto-implemented property.
I have a class which is not thread safe: class Foo { /* Abstract
I have a Queue object that I need to ensure is thread-safe. Would it
Assume I have a thread-safe collection of Things (call it a ThingList), and I
i need a thread safe map, i have something like this: (i'm very new
I have a class like the below and am wondering, will this be thread-safe
I have an network application which uses a select loop like this: bool shutdown=false;
Let's say we have a thread-safe compare-and-swap function like long CAS(long * Dest ,long
I want to have a generic thread safe collection and I saw that the
I have a custom collection (a thread-safe ObservableQueue). I implemented the business logic inside

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.