Exception while creating WPF window on different thread

I have a WPF application and I am running the animation on a different thread, so my main UI thread will be responsive. I am using the injected code here :

Thread thread = new Thread(() =>
{
    Window1 w = new Window1();
    w.Show();

    w.Closed += (sender2, e2) => w.Dispatcher.InvokeShutdown();

    System.Windows.Threading.Dispatcher.Run();
});

thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

      

It usually works fine, but after deploying the system, I got an application crash complaint 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()

      

Has anyone seen this exception before and can explain what's going on there? What could be causing this particular exception?
I am using .Net 3.5 SP1

+3


source to share


1 answer


It looks like it is System.Windows.Application.LoadComponent

not thread safe, so your call to the Window constructor might throw an error.



You can try to instantiate windows on the main thread and just show it on a new thread, but I'm not sure if that suits your application's requirements.

+1


source







All Articles