You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BAML readers in Application.LoadContent() are not protected with locks, resulting in showstopper exceptions #3411
BAML readers in Application.LoadContent() are not being properly protected with locks. This prevents windows with complex content from opening simultaneously on multiple separate threads because they crash with unhandled exceptions. It is almost impossibly difficult to lock this functionality at the application level. This bug is perfectly easy to demonstrate. This applies to any and all versions of WPF.
changed the title [-]BAML readers in Application.LoadContent() are not being properly protected with locks[/-][+]BAML readers in Application.LoadContent() are not protected with locks, resulting in showstopper exceptions[/+]on Aug 29, 2020
Could you provide a minimal repro application? Thanks.
Too busy to create a repro yet but stay tuned.
This decade-old post on Stack Overflow illustrates another aspect of the problem. Again, the workaround offered is almost impossible because Application.LoadComponent() is wrapped inside autogenerated InitializeComponent() calls. This strategy is also prone to mystery deadlocks when deep objects in the XAML graph access dependency properties on objects belonging to other threads.
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:
Checks its internal cache for the package to see if the part is already loaded & returns it if found
Loads the part from the file
Adds it to the internal cache
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:
Activity
[-]BAML readers in Application.LoadContent() are not being properly protected with locks[/-][+]BAML readers in Application.LoadContent() are not protected with locks, resulting in showstopper exceptions[/+]ryalanms commentedon Aug 31, 2020
Could you provide a minimal repro application? Thanks.
ClosetBugSlayer commentedon Sep 18, 2020
Too busy to create a repro yet but stay tuned.
This decade-old post on Stack Overflow illustrates another aspect of the problem. Again, the workaround offered is almost impossible because Application.LoadComponent() is wrapped inside autogenerated InitializeComponent() calls. This strategy is also prone to mystery deadlocks when deep objects in the XAML graph access dependency properties on objects belonging to other threads.
https://stackoverflow.com/questions/2463822/threading-errors-with-application-loadcomponent-key-already-exists
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:
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:
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:
ryalanms commentedon Aug 27, 2021
Fixed by @SamBent.