Preload WPF Window (possibly in a different thread) for better performance

I have a WPF window that takes a long time to create and show. The user should be able to open one or more of these windows. Now I am looking for a way to improve performance. One idea is to create two of these Windows when the application starts (when it displays a splash screen). After I only need to show and hide these windows (and change the attached viewing model). This should not be a problem. But when the user works with the application, and he uses two windows that load when the application starts, I have to load the third instance of the window in the background. Now the application should not be blocked when it loads the third (or 4. oder 5., etc.). Now I am looking for a way to do this.Is it possible to load a window into another stream and after transferring it to the main stream of the user interface? Or are there other scenarios to achieve the goal?

Thanks for any help.

Best regards, Thomas

+3
source share
6 answers

When you create a WPF object β€” Window , UserControl, or anything else that comes from DispatcherObject β€” it is blocked in the stream in which it was created. Thus, you cannot load a window in the background thread and then transfer it to the main user interface thread. If you are going to create it in a background thread, it should remain on that thread; it should be shown by this thread and have a message about this thread.

. , , , : ", ". Application.Run, .

, . , , ( ). . - , - . , , ( " ", , " " ). , . .

, , - . , , , .

+5

wpf, (2 ), , , . ( ). , App.xaml.cs:

        var thread = new Thread(() =>
        {//Force runtime to pre-load all resources for secondary windows so they will load as fast as possible
            new SecondaryForm1();
            new SecondaryForm2();
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Priority = ThreadPriority.Lowest;//We don't want prefetching to delay showing of primary window
        thread.Start();
        //Now start primary window

10 . , .. ? , , , , . .

+6

, ? , , , , . , , , / -/ .. , .

+4

, , WPF - .

GUI, , ? VM, ...

+1

, , , . , , - , , .

+1

Watch what Taras said. Typically, technologies such as winforms and wpf are fast enough to create a user interface. When you see a slow interface, double-check your own code: you load tens of thousands of lines (then use paging). You call the slow method (call it asynchronously) do you make many calls to the database or to the network? (bach calls or put them in another thread)

Return to what you are currently trying to do if you fail in something similar to the list above.

+1
source

All Articles