Not exactly a trick, but maybe quite interesting for some of you

Not exactly a trick, but maybe quite interesting for some of you

Originally shared by A Coders Life

10,000 User Controls Please

It's weired.. Last week we upgraded one of our clients Terminals to Windows 7. This Terminal is a PC that is used to determine the position of errors on a product while the product is being produced.

Suddenly I receive a "system error. code: 1158". All handles are being used. Handles? What for? And how many may I use?

The first horrific idea was a handle leak somewhere in the depth of our code. I did some research to find quick ways to locate this leak. I quickly stumbled upon the maximum count of handles and the fact, that I can easily see how many are being used by my application in the Task Manager. 

Nice. But after opening the Task Manager I was quite confused... The number of handles was somewhere around 300-400... Nowhere near the 10,000 (Chrome has about 3,500 right now.. ). But there was another value that attracted my attention: the amount of user controls. 10,000.

After some additional research I found out, that this can also be the reason for the 1158. But how could I possibly msolve this? The application runs 24/7 and has two thumbnail lists that are being filled with all the images I record. I looked into the function that fills my thumbnail list and I found something rather ugly... I needed to display 3 values for each thumbnail, but instead of showing them with labels, I created panels and used their caption to display the value.

Replacing the Panels with Labels was no big deals at that point, so I replaced them. Not to solve our 1158, but to avoid nightmares and paranoia.

But what I recognized then was pretty surprising: The count of user controls decreased. Not just a bit. It decreased by at least 30 - 50%! Why?!?

Pretty easy: Each Thumbnail-Element was made of the following elements: 1 Image, 4 Panels. Now it's made of these: 1 Image, 1 Panel and 3 Labels. Labels don't seem to be classical user controls, so they won't increase the user control count :)

Instead of 5 Controls, each Thumbnail now has only 2 Controls. When the error occured the first time, I had about 1600 thumbnails. That sums up to about 8,000 controls. Adding all the other controls in the application, the limit of 10,000 will easily be passed.

I was lucks to find a solution for this bug quickly, I would have had a big problem if the label would be a normal user control. This bug never appeared on Win XP. This could just have been bad luck, or the limit was introduced with WIN7...

Comments

  1. use a "window" for thumbnails that will be displayed and a buffer of say 5-10 items left and right depending on where the "window" is, you may want to buffer more to the left or right, up to you, have fun! (:

    ReplyDelete

Post a Comment