5 Replies - 775 Views - Last Post: 16 February 2013 - 07:31 AM Rate Topic: -----

#1 megmut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-February 13

Visual Studio 2010, C# : Keeping forms open, bad for performance?

Posted 15 February 2013 - 04:03 PM

Hey guys just a quick question! In vb (the good o'l days) i used to be able to just change the projects attributes so the application would exit when LAST form closes. I cant do this in c# for some reason. The only way i've been able to find to keep the application open whilst switching between forms is using the "this.hide, form2.show"

However, i feel this could be bad for performance (ram, cache) in the long run as if many forms are opened, is the program not going to be pulling more and more resources in order to keep all forms open?!

Any help would be greatly appreciated. Thanks, Nick

Is This A Good Question/Topic? 0
  • +

Replies To: Visual Studio 2010, C# : Keeping forms open, bad for performance?

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Visual Studio 2010, C# : Keeping forms open, bad for performance?

Posted 15 February 2013 - 06:27 PM

If you look at the program.cs file you'll find a line like this:
Application.Run(new Form1());

That is the only form that has to remain open. When it closes, the program ends.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Visual Studio 2010, C# : Keeping forms open, bad for performance?

Posted 15 February 2013 - 07:35 PM

Actually, you need to read about ApplicationContext and how you can have a long running process without keeping that form open. I think Jessica Fosler wrote a blog post about it. I can't search for it right now while I deal with another issue, but it has to do about creating TrayIcon apps in .NET.

Here's the link to her blog post:
http://blogs.msdn.co.../24/455579.aspx

This post has been edited by Skydiver: 15 February 2013 - 07:40 PM

Was This Post Helpful? 1
  • +
  • -

#4 megmut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-February 13

Re: Visual Studio 2010, C# : Keeping forms open, bad for performance?

Posted 16 February 2013 - 06:28 AM

Thanks guys :) Just out of curiosity, im new here.. does this forum run on some sort of point basis for good/helpful answers? if so how do i give? :P
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Visual Studio 2010, C# : Keeping forms open, bad for performance?

Posted 16 February 2013 - 06:32 AM

Hit the green + button.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Visual Studio 2010, C# : Keeping forms open, bad for performance?

Posted 16 February 2013 - 07:31 AM

View Postmegmut, on 15 February 2013 - 05:03 PM, said:

However, i feel this could be bad for performance (ram, cache) in the long run as if many forms are opened, is the program not going to be pulling more and more resources in order to keep all forms open?!


Will forms takes up memory? Yes.
But in today's modern multi-gig computers its like throwing bricks into the Grand Canyon: Technically its filling it up, but in reality it makes little difference.

Other larger objects would have a greater affect. Like loading 2,000 high-res 18mp images from your camera into memory all at once. This sort of thing is where people get themselves in trouble. They make objects that remain in scope so they don't get disposed of by the GC. Or unmanaged objects like brushes for painting that don't get disposed of, used in the ON_PAINT method so they are made hundreds of times in a few minutes.


I'm more interested in your program architecture. You say you want to have the program close when the LAST window is closed. This sounds like very linear thinking instead of hierarchical thinking. For example opening a Login screen, then opening the main window - so the main window is really the second window to open. You may have to get used to the idea that you don't want to code in the same order the user sees things on screen.

In .NET we think more hierarchical... more about the parent<->child relationship. The main window will spawn child windows. Every GUI control on your window is a child of the form. If you nest controls inside other controls (buttons inside a groupbox for example) then the relationship grows to form->groupbox->button.

Your applications revolves around the first window opened as Momerath pointed out. Your main windows should be the first thing that opens. Its the parent. It is the window that your application considers the important one that will cause the application to exit, when it is closed. Again think parent->child. When mom leaves the grocery store all the kids leave too.

So open the main window first. Have *it* open the log-in dialog. If the user successfully logs in then you can set up your main window based on their access level. If they fail log in, then close the app or keep the log-in open until someone does log in successfully.

If you are trying to create more of a Wizard do a step-by-step through lots of dialogs...
Use the main window as the controller. You don't have to show the main window to have its code running. Have it launch, hide, then start walking through form2, form3, form4...form87 and so on.

Personally, I'm not a fan of lots of windows. I think it feels very 1987. People now days expect one window with the content to change. You can do this by creating UserControls. Think of a UserControl as 99% of a Form, but without the border and titlebar. You can then keep one form open, but swap out UserControls in the center of it. Think how TurboTax keeps one frame, but walks you through lots of screens of questions when doing your taxes.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1