0

I'm making a program that has a login window and the main window. I am wondering if hiding windows would have more impact on performance than closing them.

I've written down both these options, let me know if there's a better way to do it.

private void Login()
{
    DataMatrixWindow dmWindow = new DataMatrixWindow(); // Creates new Datamatrix window
    dmWindow.Show();
    Close(); // Closes login window
}

Or keeping Login window alive the whole time, and just show / hide it when needed.

private void Login()
{
    DataMatrixWindow dmWindow = new DataMatrixWindow(); // Creates new Datamatrix window
    dmWindow.Show();
    Visibility = Visibility.Collapsed; // Hides login window
}

MSDN makes no note of a potential performance hit if windows are not closed.

Window.Close Method: Unmanaged resources created by the Window are disposed.

Window.Closing Event: If you want to show and hide a window multiple times during the lifetime of an application, and you don't want to re-instantiate the window each time you show it, you can handle the Closing event, cancel it, and call the Hide method. Then, you can call Show on the same instance to re-open it.

Some more details

  • I am closing and creating a new Datamatrix window, though the same arguments could be made whether to show or hide it like the login screen
  • I do not expect the user to constantly log in and out, so these switches shouldn't occur very often (Hence why I'm leaning to closing the login window instead of hiding it)
Valrog
  • 172
  • 1
  • 1
  • 11
  • 3
    My opinion: close it. – Clemens Oct 25 '18 at 11:35
  • 2
    `MSDN makes no note of a potential performance hit if windows are not closed.` The performance impact is that the window will still exist (i.e. not be closed), just hidden. Whether this is an issue for you depends on what the window contains, how memory constrained your machine is, how long the window takes to load vs show etc etc. – mjwills Oct 25 '18 at 11:55
  • You are not going to notice a perf or OS resource impact for just a single window. But as with everything, this can get badly out of hand if you do it too much. [This question](https://stackoverflow.com/q/52969380/17034) was notable that way. – Hans Passant Oct 25 '18 at 14:28

2 Answers2

2

When you use Visibility.Collapsed, it does not remove your window from the visual tree, therefore there is performance implication on leaving it unseen. To see this performance implications yourself you can do 2 things:

  1. Open Snoop and you will be able to see the entire window on the visual tree.
  2. Create a tester and fill it with buttons (for example) through code - place 10 thousand and set them with Visibility.Collapsed. Then check the performance there as apposed to a clean application. see how long does it take for it to show when starting the exe.

Hope this helps.

Zeus
  • 61
  • 5
1

I am wondering if hiding windows would have more impact on performance than closing them.

Probably. Closing and showing a simple window such as a login form shouldn't impact the performance noticeably so I would recommend you to close the window when you have logged in and show another instance of it when and if you need to log in again. This is certainly better than keeping the window open in the background after you have logged in. At least in my opinion.

mm8
  • 163,881
  • 10
  • 57
  • 88