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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:38:33+00:00 2026-05-13T23:38:33+00:00

MSDN says that public static members of System.Windows.Application are thread safe. But when I

  • 0

MSDN says that public static members of System.Windows.Application are thread safe. But when I try to run my app with multiple threads I get the following exception:

ArgumentException: An entry with the same key already exists.

   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.SortedList`2.Add(TKey key, TValue value)
   at System.IO.Packaging.Package.AddIfNoPrefixCollisionDetected(ValidatedPartUri partUri,
        PackagePart part)
   at System.IO.Packaging.Package.GetPartHelper(Uri partUri)
   at System.IO.Packaging.Package.GetPart(Uri partUri)
   at System.Windows.Application.GetResourceOrContentPart(Uri uri)
   at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean 
bSkipJournaledProperties)
       at System.Windows.Application.LoadComponent(Uri resourceLocator)

The exception occurs on the following call:

genericResources = (ResourceDictionary)Application.LoadComponent(new Uri("/Themes/Generic.xaml", UriKind.Relative));

The application works fine on a single thread and even on two or three. When I get up past 5 then I get the error every time. Am I doing something wrong? What can I do to fix this?

  • 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-13T23:38:33+00:00Added an answer on May 13, 2026 at 11:38 pm

    You are not doing something wrong. MSDN is wrong. Application.LoadComponent is not actually thread safe. This is a bug in WPF, in my opinion.

    The problem is that whenever Application.LoadComponent loads a “Part” from a “Package” it:

    1. Checks its internal cache for the package to see if the part is already loaded & returns it if found
    2. Loads the part from the file
    3. Adds it to the internal cache
    4. Returns it

    You have two threads calling Application.LoadComponent to load the same part at the same time. The MSDN documentation says this is ok, but what is happening is:

    1. Thread #1 checks the cache and starts loading from the file
    2. Thread #2 checks the cache and starts loading from the file
    3. Thread #1 finishes loading from the file and adds to the cache
    4. Thread #2 finishes loading from the file and tries to add to the cache, resulting in a duplicate key exception

    The workaround for the bug is to wrap all calls to Application.LoadComponent inside a lock().

    Your lock object can be created thusly in your App.cs or elsewhere (your choice):

     public static object MyLoadComponentLock = new Object();
    

    Then your LoadComponent call looks like this:

     lock(App.MyLoadComponentLock)
       genericDictionary = (ResourceDictionary)Application.LoadComponent(...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

msdn: Any public static (Shared in Visual Basic) members of this type are thread
What is the significance of the Thread.Join method in C#? MSDN says that it
Reading msdn documentation for dictionaries it says : Public static (Shared in Visual Basic)
The MSDN documentation says that public class SomeObject { public void SomeOperation() { lock(this)
MSDN says that after CryptVerifySignature() you cannot reuse HCRYPTHASH object to try hashing more
The MSDN says that using ReadDirectoryChangesW implies the calling process having the Backup and
The MSDN site says: From your view class's function that handles the WM_CREATE message
I've read about advantages of Tasks Difference between Task (System.Threading.Task) and Thread Also msdn
MSDN says that GC calls the Win32 VirtualAlloc function to reserve segment of memory
From this MSDN article it says that the connection string for connecting to the

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.