0

I am creating a few windows forms with many controls on it asynchronously (each on a thread created with a sole purpose to open that form).

for(var i = 0; i < 10; i++)
{
    var thread = new Thread(() =>
    {
        var form = new FormX();
        form.ShowDialog();
    });
    thread.Start();
}

I always get this error "Error Creating Window Handle". I tried googling that the limit is 10.000 handles. However, I have another thread that checks the amount of handles like this:

var handleThread = new Thread(() =>
{
    while(true)
    {
        System.Diagnostics.Debug.WriteLine(System.Diagnostics.Process.GetCurrentProcess().HandleCount);
    }
});
handleThread.Start();

At maximum it prints out like 800, which is nowhere near the limit.

What could be the problem?

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
  • 2
    The **default** limit is 10,000. Did you check your actual limit? Also, this might be related to the maximum number of User Objects. – Zohar Peled Jan 19 '17 at 09:59
  • 1. How do I check my application setting for the limit? 2. How do I check the amount for user objects? – Andrius Naruševičius Jan 19 '17 at 10:05
  • read [this](https://blogs.technet.microsoft.com/markrussinovich/2009/09/29/pushing-the-limits-of-windows-handles/) and [that](https://msdn.microsoft.com/en-us/library/windows/desktop/ms725486(v=vs.85).aspx). – Zohar Peled Jan 19 '17 at 10:14

1 Answers1

0

I thought that graphical manipulations had to be done in the main thread. I would have expected your code to crash much faster. Did you try running it in the main thread to see how many windows you can create?

Bruno Belmondo
  • 2,299
  • 8
  • 18
  • I need to create a lot of forms fast. They take a few seconds to load, I really don't care for them to appear as it is a test whether they load successfully or not - I do close them immediately after. – Andrius Naruševičius Jan 19 '17 at 10:53