Maximum Window Handles

  • Thread starter Thread starter Avi Elenko
  • Start date Start date
A

Avi Elenko

When creating a WinForm control containing ~9600 (96K)
controls, a System.ComponentModel.Win32Exception with
NativeErrorCode set to 14 (0xE)
ERROR_OUTOFMEMORY "Not enough storage is available to
complete this operation" is thrown. Can please help how
one can bypass this limitation. I'm attaching sample c#
code that will recreate this problem. Please note that
once this exception is thrown, no new windows can be
created for this thread/form. In other words, the
MessageBox within catch statement will never show.
Thanks!

System.Windows.Forms.Panel mainPanel = new
System.Windows.Forms.Panel();
mainPanel.SuspendLayout();
try
{
for (int i=0; i<5000; ++i ) // Will break around 9700
(97K)
{
Panel p =new Panel(); p.Width =
this.ClientRectangle.Width; p.Height = 20; p.Top = i * 20;
p.Left = 0;
TextBox t = new TextBox();t.Text = ">>> Name= " +
i; t.Size = p.Size;
p.Controls.Add( t ); mainPanel.Controls.Add(
p );
}
}
catch ( System.Exception ex ){ MessageBox.Show( ex.ToString
() ); }
 
* "Avi Elenko said:
When creating a WinForm control containing ~9600 (96K)

ROTFL. 9600 controls? Every control has a window? Try to reduce the
number of controls needed. I would recommend to use < 256 controls on a
form to get acceptable performance and memory usage.
 
I was not asking for an opinion but a work
around/solution. Seems like each WinForms control aka
System.Windows.Forms.Control is using a Win32
Handle/Window. Well, imagine a Tree Grid control with
RichTextBox in each cell/column. So when creating say
2500 rows * 2 columns, at least 10K handles will be
created, and that's when WinForms/Win32 breaks. Btw - it
works wicked fast on P3 866 Mhz laptop
 
* "Avi Elenko said:
I was not asking for an opinion but a work
around/solution. Seems like each WinForms control aka
System.Windows.Forms.Control is using a Win32
Handle/Window. Well, imagine a Tree Grid control with
RichTextBox in each cell/column. So when creating say
2500 rows * 2 columns, at least 10K handles will be
created, and that's when WinForms/Win32 breaks. Btw - it
works wicked fast on P3 866 Mhz laptop

Mhm... I would consider that to be bad design. Instead of using
RichTextBoxes for each cell, it's preferred to draw the data onto the
cells and move a single RichTextBox onto the "active" cell.
 
Same conclusion I came to before my initial post, but that
implies that one need to implement an RTF reader/parser,
do all drwaings etc., which is kind of counterproductive.
Thanks at any rate, your comments helped me in making the
decision I was trying to avoid.
 
* "Avi Elenko said:
Same conclusion I came to before my initial post, but that
implies that one need to implement an RTF reader/parser,
do all drwaings etc., which is kind of counterproductive.
Thanks at any rate, your comments helped me in making the
decision I was trying to avoid.

I felt sorry to say that, but I know that it's really hard to implement
something like what you want to implement...
 
Well then why not just create enough RichTextBoxes for the cells that are
visible? There are several ways to approach this, but you might try to
keep a cache of so many RichTextBox controls, creating more only as they are
needed. You may have to do a lot of swapping out of data, but this would
certainly be a better idea than creating 5000 controls. And unless your
user is using some really high resolution, you shouldn't run out of window
handles (you could even restrict the number of visible rows your controls is
CAPABLE of displaying).

But before you do this, are you certain that you need ALL of the
functionality of RTF? If you only need a few formatting tags, then you
could easily write your own XML-based markup language and write a renderer
for it. RTF supports practically every formatting feature of Microsoft
Word. Do you really need all that? (Think: columns, tables, OLE objects,
headers, footers, images, calculated fields, TOC, footnotes).

--Matthew W. Jackson
 
Back
Top