2

I am writing WinForms application, and so far it contains 37 forms. This is because of my project's needs.

My questions are:

  1. Is there a limitation to the number of forms created in C#?
  2. Does it have an effect on application performance?

Thanks for your help.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Real2000
  • 63
  • 1
  • 5

4 Answers4

8

Is there a limitation in forms creation in C# ?

AFAIK there is no such limitation imposed by the the .NET Framework. Obviously the more forms you create and keep in memory, the less memory you will have until you run out of it. And the less memory you have could lead to slower performance.

On the other hand if you keep in memory only the current form and leave the GC take care of the others by leaving their instances fall out of scope there won't be such problems.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

There is no limit on number of forms until you run out of ram. It will affect both system and application performance though.

Daniel
  • 30,896
  • 18
  • 85
  • 139
0

not that I know of, or other modules or classes within a project can create as many you'll need without any restrictions.

Regards.

  • but on the other side we will have memory loss ! – Real2000 Aug 31 '11 at 06:10
  • Hello, sorry for my bad English because then I realized-level designers, but if you intend to open several at runtime, then I agree with you, when there will be more free memory you have an error in OutOfMemory exception.Regards – Carmelo La Monica Aug 31 '11 at 06:38
  • you mean with large number of forms (i.e up to 50) if we don't open many forms at a time,we won't have memory problems ? – Real2000 Aug 31 '11 at 09:41
0

I had an project with too many winforms in it(Approx 50). After 2 or 3 Debug Builds and Runs i used to get following error.

Error 1   Unexpected error writing metadata to file

'E:\Repository\Project\JewelSoft\PresentationTier\obj\Release\PresentationTier.exe' -- 'Not enough storage is available to complete this operation. ' PresentationTier

So take care and always implement GC where ever required.

Update 1: Although there are methods like GC.Collect(), you dont often require to implement it unless you have cases that your code ends abruptly or switches the threads. Things like Disposing the forms which are not required after closing and avoiding repetitive declarations of object will be helpful.

Update 2: To know more when are where to write GC.Collect please refer these links:

  1. GC.Collect()

  2. When to call GC.Collect

  3. When is it acceptable to call GC.Collect?

And about form dispose, on Form close method it will automatically get disposed, but then if you have an variable for that form in some other form then it is advisable to write form1 = null; in form1_disposed event

Community
  • 1
  • 1
Marshal
  • 6,551
  • 13
  • 55
  • 91
  • can you please help me how can i work with GC ? because of knowing this fact that .NET Automatically moderates GC i have not been worked with GC ! thank you ! – Real2000 Aug 31 '11 at 06:32
  • @Real2000: Although there are methods like GC.Collect(), you dont often require to implement it unless you have cases that your code ends abruptly or switches the threads. Things like Disposing the forms which are not required after closing and avoiding repetitive declarations of object will be helpful – Marshal Aug 31 '11 at 06:41
  • I don't see what a GC.Collect would offer since the equivalent is running if the system is running low on memory. If that does not free enough bytes, a manual GC.collect call won't, too. – TheBlastOne Aug 31 '11 at 06:58
  • where should i write GC.Collect() ?! and i should add form.Dispose in form closed event ? – Real2000 Sep 01 '11 at 09:11
  • @Real2000: Please refer the Update 2 – Marshal Sep 02 '11 at 12:14