It usually works fine, but after the system was deployed I got complaint about application crash with the following stack trace:

System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List`1.RemoveAt(Int32 index)
at System.IO.Packaging.PackagePart.CleanUpRequestedStreamsList()
at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
at System.IO.Packaging.PackagePart.GetStream()
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at Window1.xaml:line 1
at Window1..ctor()

Have anyone seen this exception before and can explain what is going on there? What could be the reason for this specific exception?
I'm using .Net 3.5 SP1

1 Answer
1

It looks like System.Windows.Application.LoadComponent is not thread-safe so your call to Window constructor can cause error. You can try to create window instance in main thread and just show it in new thread, but a i am not sure if it fits to your application needs

Sounds like a solution :-) Do you have any additional reference to back up this assumption? I have no way to test it, since it only happened once in the field...
–
Amittai ShapiraApr 10 '12 at 13:33

No, this is just an idea. You can take a look at code of System.Windows.Application.LoadComponent to find thread-safety issues there. Or you can try to stress test LoadComponent with test that creates many windows simultaneously on different threads to reproduce this bug
–
NikolayApr 10 '12 at 14:57

Ok, thanks. I'll take a look at it - I've upvoted you, and will accept this answer when I'll be sure it's the real root cause of the exception I've had.
–
Amittai ShapiraApr 12 '12 at 12:16

It seems that this was the problem - creating many such user controls in a seperate thread does fail from time to time. I've added lock on the user controls creation - Thanks!
–
Amittai ShapiraApr 16 '12 at 12:31