vb.net Error creating window handle

Hi All expert,
I have no ideal why and when this error happen every day. I cannot similate the error. Do your have any ideal?

Error creating window handle.System.Windows.FormsSystem.ComponentModel.Win32Exception: Error creating window handle.
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.CreateGraphicsInternal()
   at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
   at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
   at System.Windows.Forms.ThreadContext.OnThreadException(Exception t)
   at System.Windows.Forms.Control.WndProcException(Exception e)
   at System.Windows.Forms.ControlNativeWindow.OnThreadException(Exception e)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It's probably because a lack of memory... Try to cleanup as much as you can of what you allocate...

Take a look at this:

Eduard GherguLeader SibiuCommented:

The problem is that your application is creating too many objects (windows NT/XP has a limit of 10,000 windwos handles per process). Try to redesing your application in order to create only the required objects for a certain step. Also, try to avoid to use too many global objects.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
delphikitAuthor Commented:
Dear NeoEGM,

It could be this reason. Anywhere I will try it 1st. But i notice that the memory Usage always go between 40k to 60k. Is there any where to cut it down?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

A way to know more about your process is by going to Windows Task Manager->"Processes" tab->"View" menu->Select columns->Check:

- USER objects
- Handle Count
- Thread Count
- GDI Objects

You should try to keep them as low as possible...

Don't worry so much about the memory usage of the .NET Framework... VB.NET uses to allocate lots of memory and it isn't always a problem... you should reduce the amount of handles/objects in use; it's the best place to start from...

delphikitAuthor Commented:
Dear NeoEGM,

Thank you again for your reply. I had tested few transaction in my application and this is the result i get from the Windows Task Manager.
 USER objects = 307
 Handle Count =485
 Thread Count =11
 GDI Objects =679

It is normal?.. What is the limit for thoese usage?


Dear dephikit,

It don't know what kind of application you're developing, but I dare to say that the GDI Objects, the USER Objects and the Handle Count are too high...

If I were you I'd take a look at all my code to see if I close all the handles I open/get, if I free all the GDI objects I get allocated and if I do the needed cleanup after any operation I finish...

Probably, when those values get higher, the app will throw the error you described...

Keep monitoring the values as you do specific operations on your app and try to cleanup your code as much as you can...

FYI. I was getting this error repeatedly and intermittently as well. I was not creating any wnew window handles, the only window was the single dialog.  but for my application I would, for each file in a queue...

a. position the cursor offscreen
b. lock the cursor using a windows API
c. call an external application that I also wrote

It was this context swtching that caused the problem, occasionally a vibration would cause the mouse to try to move while it was closing the external application and switching back to my dialog application - outside of the context of my application the mouse was no longer locked, if it was moving when it switched back i somethimes got this error. By disabling the mouse lock the problem completely disappeared.

So it was while my window was taking the focus that this error occurred for me and you should be aware that it can be dificult to determine the root cause. I have put it down as a dotnet foible.

You aren't by any chance placing a private class in the form1.vb file above the definition of the form are you?  This has bitten me before, but I don't recall the error message.
delphikitAuthor Commented:
Hi Jobrienct,

How to position the cursor offscreen and how to lock the cursor using windows API?
Can you send me the code how to do that?

sure, sounds a bit OT for your Q but ...

Dim iWidth As Integer = 720
Dim iHeght As Integer = 480
Cursor.Position = PointToScreen(New Point(iWidth + 10, iHeight + 10))

will position your cursor off and to the bottom right of your screen. I use an API to set the screen resolution when the prgram starts.

more later
In declarations add:

Friend Mouse As New MouseTrap

Then use the following class to disable...


or enable...


your mouse - which either allows or disallows mouse movements while your application has the focus.

Public Class MouseTrap

#Region "Declarations - Constants"

    Private Const HC_ACTION As Integer = 0
    Private Const WH_MOUSE_LL As Integer = 14
    Private Const WM_MOUSEMOVE As Integer = &H200
    Private Const WM_LBUTTONDOWN As Integer = &H201
    Private Const WM_LBUTTONUP As Integer = &H202
    Private Const WM_LBUTTONDBLCLK As Integer = &H203
    Private Const WM_RBUTTONDOWN As Integer = &H204
    Private Const WM_RBUTTONUP As Integer = &H205
    Private Const WM_RBUTTONDBLCLK As Integer = &H206
    Private Const WM_MBUTTONDOWN As Integer = &H207
    Private Const WM_MBUTTONUP As Integer = &H208
    Private Const WM_MBUTTONDBLCLK As Integer = &H209
    Private Const WM_MOUSEWHEEL As Integer = &H20A

#End Region

#Region "Declarations - Structures"

    Private Structure POINT

        Private x As Integer
        Private y As Integer

    End Structure

    Private Structure MSLLHOOKSTRUCT

        Private pt As POINT
        Private mouseData As Integer
        Private flags As Integer
        Private time As Integer
        Private dwExtraInfo As Integer

    End Structure

#End Region

#Region "Declarations - Win32 API's"

    Private Declare Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Integer, _
        ByVal lpfn As LowLevelMouseProcDelegate, _
        ByVal hmod As Integer, _
        ByVal dwThreadId As Integer) As Integer

    Private Declare Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Integer, _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As MSLLHOOKSTRUCT) As Integer

    Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Integer) As Integer

#End Region

    Private hhkLowLevelMouse As Integer

#Region "Declarations - Functions"

    Private Function LowLevelMouseProc( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As MSLLHOOKSTRUCT) As Integer

        If (nCode = HC_ACTION) Then

            If wParam = WM_MOUSEMOVE Or _
                wParam = WM_LBUTTONDOWN Or _
                wParam = WM_LBUTTONUP Or _
                wParam = WM_LBUTTONDBLCLK Or _
                wParam = WM_RBUTTONDOWN Or _
                wParam = WM_RBUTTONUP Or _
                wParam = WM_RBUTTONDBLCLK Or _
                wParam = WM_MBUTTONDOWN Or _
                wParam = WM_MBUTTONUP Or _
                wParam = WM_MBUTTONDBLCLK Or _
                wParam = WM_MOUSEWHEEL Then

                Return 1

            End If

            Return CallNextHookEx(hhkLowLevelMouse, _
                nCode, wParam, lParam)

        End If

    End Function

    Private Delegate Function LowLevelMouseProcDelegate( _
        ByVal nCode As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As MSLLHOOKSTRUCT) As Integer

#End Region

#Region "Declarations - Mouse Public Class Methods"

    Public Sub Disable()

        'Set MouseTrap
        hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, _
            AddressOf LowLevelMouseProc, _
            Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, _

    End Sub

    Public Sub Enable()

        'Release MouseTrap

    End Sub

#End Region

End Class ' Class MouseTrap
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.