简体   繁体   中英

WPF application gets slow after opening and closing windows

Every time I open a new window I notice a drop in performance. When I open a new window, I use this:

        NewWindow.Show();
        Window oldWindow = Application.Current.MainWindow;
        Application.Current.MainWindow = NewWindow;
        oldWindow.Close();

Does this cause the references/drop in performance, or do i need to dive deeper into this application?

Edit: was a horrible CanExecute that was polling the database.

I had the same problem and solved it by creating new window in new thread! Here is example:

 Thread windowThread = new Thread(new ThreadStart(() =>
        {
            MyWindow NSWindow = new MyWindow();

            // When the window closes, shut down the dispatcher
            NSWindow.Closed += (s, e) =>
            Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
            NSWindow.Show();
            // Start the Dispatcher Processing
            System.Windows.Threading.Dispatcher.Run();
        }));

        windowThread.SetApartmentState(ApartmentState.STA);
        // Make the thread a background thread
        windowThread.IsBackground = true;
        // Start the thread
        windowThread.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM