"Error Creating Window Handle" while creating multiple forms

306 views Asked by At

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?

1

There are 1 answers

1
Bruno Belmondo On

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?