"Error creating window handle" - c#

I'm getting this nasty 'error creating window handle' during the startup itself i.e the login form. I would have looked into the optimizing the controls if it would have come after the UI is up. This is coming during the load of first form which basically has only login & password textbox controls.
The program is C# winforms.

I probably need more information but my guess is that you have a memory leak somewhere in your program. Try running Task Manager to see if you have a thread or handle growing massive.
If you do, you probably need to dispose some UI elements somewhere in your code.

Related

Create form not affected by ShowDialog()

I'm currently doing a security assessment of an extremely large C# application. I have wrote a tool to help me do the assessment that also runs in C#. The entire application uses ShowDialog aka modal forms. I'm trying to come up with a way where I can still use my tool (click on it for example) while modal dialog boxes are popped up. I figure, this may require another thread or something else. If anyone has any easy tips on a way to make it so that my form doesn't get blocked by ShowDialog, that would be great.
The entire purpose in using a modal dialog is for it to block it's parent until it is dismissed.

WinForm: Loading form for InitializeComponent method

We have a Windows Form application for our fingerprint scanning for time keeping and it takes too much time for just initializing the control for the finger print scanning, around 5 to 10 minutes. So we need to create a loading form so that users will know that the application doesn't hang and they need to wait, but the cause of the long running process was inside the InitializeComponent() method from the initialization of the form.
My question is, how can I move the InitializeComponent() method to another thread so that I can show a loading form while initializing the form where the biometrics scanner control is, because it creates an error when accessing a UI control from another thread other than the main thread?
My code is in C#, Framework 2.0, build in Visual Studio 2008.
Please help, thanks in advance.
You can remove your control for the finger print scanning from the visual designer and create this control from the code using an additional background thread and writing a progress information during this process.
SO has lot of data about it:
How to update the GUI from another thread in C#?
Accessing UI in a thread
Best Way to Invoke Any Cross-Threaded Code?
you can find a lot more.
If still need help, post a comment.
You can find your InitializeCompoent(); method in yourForm.designer.cs, and from there you can modify it.

Small flickering when showing a modal dialog in .NET

I'm experiencing a annoying issue in my .NET Windows Forms application.
I have a MainForm (A) and a progress form (B) that is shown modal when I start a long operation.
When the operation finishes, and B is closed, it seems that the window that is behind my application (is occurs usually with Skype) is brought to front during few milliseconds and, then my application is activated normally.
Is only a small flickering, but annoying. Here I write some tips that could help to find the solution:
I called showDialog without IWin32Window, but I added it without success result.
I closed B using Close(), then I tried Hide() and then Close() but did not help.
The code that executes the ShowDialog() is invoked from the UI thread, so it don't seem to be a threading issue.
Any clue would be appreciated. Thanks in advance.
I was experiencing these same symptoms and it was driving me crazy.
I finally discovered the problem was that i was calling this.Dispose() instead of this.Close() to close the modal window I'm not sure why I called this.Dispose() in the first place.
After switching methods, the problem went away.
I realize that this thread is old and this is not the cause of your problem, i am just trying to help anyone else who made the same mistake that I did.
Check if you DON'T call Hide or Close. The only way to avoid flickering is DialogResult.
Upd:
this.DialogResult = DialogResult.Ok
Check handlers OnFormClosing and etc. They might containt wrong method call.
The little trick is to set Owner explicitly
_dialog.Owner = this;
_dialog.ShowDialog();
People who are editing the post - _dialog.ShowDialog(this) works a bit different.
Look at owner = ((Control) owner).TopLevelControlInternal; in decompiled code
Edited by someone:
Or...
_dialog.ShowDialog(this);
These calls are identical according to MSDN
Do not call Close for modal window (it will not be disposed and memory leak is guaranteed)
Set this.DialogResult = DialogResult.OK
Call Dispose() from the parent, NEVER from the form you are closing
Remember of disposing all of your components holding by the form IContainer in Dispose(bool) (VS Designer implementation of Dispose(bool) is usually not enough not to have memory leaks)
In my case I was also facing the same issue in my VB.Net winform application but a bit different scenario.
I was having a user control which opens up a dialog using showdialog() say dialog1 and on filling some data it hides dialog1 and opens up dialog2 using showdialog() again.
In the process of hiding dialog1 and showing dialog2 flickering occurs and it shows the window at background for a moment.
After trying so many solutions and workarounds none work for me. I found one workaround myself which might help others.
To hide dialog1 I was using Me.Hide(), The solution is to change the opacity of form instead of calling Hide() method.
'Me.Hide()
Me.Opacity = 0
After this workaround the application works fine without any flickering issue.
PS: The above code lines are in VB.Net but enough for a .Net winform application developer to get the idea for fixing this issue.
Okay,
it sounds like
In the main window a user starts a long operation
You display a progress modal window
Operation completes you close the progress window
Your main window doesn't display immediately. Instead something behind it shows through for a second or less.
Your main window completes it's redraw operation and is 100% visible.
This happens more often when you have applications such as Skype running.
If that's the case there are many different possible causes. For example, your video drivers might have a bug in them causing delays in off screen compositing under certain conditions.
The system itself might even be experiencing a blocking CPU operation at the moment. This is something that could be caused by the time it takes your code to close the dialog and go back to the main form. You might look to see if there is anything else you are doing between the time the progress closes and you return UI control back to the user.
The system might simply be memory constrained and your operation causes a huge swap to disk. When the operation completes, windows might be notified that it needs to pull previously swapped memory from disk and shove it back into RAM, causing a delay.
I'd run the app with Nothing else loaded but task manager or resource monitor and see what happens. If the problem no longer occurs, then look into adding more RAM to your machine and/or ignore it.
If it's still occuring, and your memory usage leaves nearly nothing left then, again, add RAM or ignore.
If it's still occuring but memory usage is low, investigate your code to see what you are doing between the closing of the dialog and release of control of the main window back to the user.
You could try double buffering your form with this example:
Tends to eliminate flicker on screen updates.

Need Suggestion for my app

I want to show an loading image when i launch my desktop application. can anybody tell me how to show this in C#? Results required as happens in visual studio 2005/2008 when we launch this application.
What you are looking for is actually a splash screen. Look at this codeproject article. It explains really well how you should do this.
http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx
What i did in my previous project was, that make the Form1 (display form) and make it wait for a while, then close it after that launch the Form2 which is the actual application.
But that was way back when i was a fresher. Now what i might do is, have a Form1 which ill launch and in the backgroundworker do all major UI processing work such as if any loading of data needs to be done, etc stuffs and keep every thing ready in memory. Then ill close the Form1 or make it hide or invisible and launch the Form2 which is the main app form and pass all the information required to it.

Winforms issue - Error creating window handle [duplicate]

This question already has answers here:
"Error Creating Window Handle"
(11 answers)
Closed 10 years ago.
We are seeing this error in a Winform application. Can anyone help on why you would see this error, and more importantly how to fix it or avoid it from happening.
System.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.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ButtonBase.OnVisibleChanged(EventArgs e)
Have you run Process Explorer or the Windows Task Manager to look at the GDI Objects, Handles, Threads and USER objects? If not, select those columns to be viewed (Task Manager choose View->Select Columns... Then run your app and take a look at those columns for that app and see if one of those is growing really large.
It might be that you've got UI components that you think are cleaned up but haven't been Disposed.
Here's a link about this that might be helpful.
Good Luck!
The windows handle limit for your application is 10,000 handles. You're getting the error because your program is creating too many handles. You'll need to find the memory leak. As other users have suggested, use a Memory Profiler. I use the .Net Memory Profiler as well. Also, make sure you're calling the dispose method on controls if you're removing them from a form before the form closes (otherwise the controls won't dispose). You'll also have to make sure that there are no events registered with the control. I myself have the same issue, and despite what I already know, I still have some memory leaks that continue to elude me..
See this post of mine about "Error creating window handle" and how it relates to USER Objects and the Desktop Heap. I provide some solutions.
This problem is almost always related to the GDI Object count, User Object count or Handle count and usually not because of an out-of-memory condition on your machine.
When I am tracking one of these bugs, I open ProcessExplorer and watch these columns: Handles, Threads, GDI Objects, USER Objects, Private Bytes, Virtual Size and Working Set.
(In my experience, the problem is usually an object leak due to an event handler holding the object and preventing it from being disposed.)
I think it's normally related to the computer running out of memory so it's not able to create any more window handles. Normally windows starts to show some strange behavior at this point as well.
I got same error in my application.I am loading many controls in single page.In button click event i am clearing the controls.clearing the controls doesnot release the controls from memory.So dispose the controls from memory.
I just commented controls.clear() method and include few lines of code to dispose the controls.
Something like this
for each ctl as control in controlcollection
ctl.dispose()
Next
Well, in my case it was definitely the USER Objects that were out of control. I looked in the Windows Task Manager and sure enough, the USER Objects count was at 10'000 exactly.
I am dynamically embedding property and list sheets in Tab Pages by setting the Parent property of the property or list sheet's container panel to that of the Tab Page. I am conditionally recycling or re-creating the property and list sheet forms depending on the type of collection being listed or class type of the object being inspected.
NB: In Delphi, all controls had an Owner and a Parent property. Even if one changed the Parent property of a control, it would still be disposed by its owner when the owning control got destroyed.
In C# it seems that if a control e.g. a Panel is programmatically reassigned from, say, a Form to a Tab Page by changing the Panel.Parent property, calling Dispose() on the Form won't dispose the Panel, neither will calling Controls.Clear() on the Tab Page. Even a direct call Panel.Dispose() won't actually dispose it, unless its Parent is manually set to null beforehand.
I added a check that makes it work...
if (_form.Handle.ToInt32() > 0)
{
_form.Invoke(method, args);
}
it is always true, but the form throws an error without it.
BTW, my handle is around 4.9 million
Definitely too many handles(memory leak issue):
IT Jungles: System.ComponentModel.Win32Exception: Error creating window handle
The out of memory suggestion doesn't seem like a bad lead.
What is your program doing that it gets this error?
Is it creating a great many windows or controls?
Does it create them programatically as opposed to at design time?
If so, do you do this in a loop? Is that loop infinite?
Are you consuming staggering boatloads of memory in some other way?
What happens when you watch the memory used by your application in task manager? Does it skyrocket to the moon? Or better yet, as suggested above use process monitor to dive into the details.

Categories