﻿<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>StrataFrame Forum » StrataFrame Application Framework - V1 » WinForms (How do I?)  » Cannot close a form in the load event</title><generator>InstantForum 2017-1 Final</generator><description>StrataFrame Forum</description><link>http://forum.strataframe.net/</link><webMaster>StrataFrame Forum</webMaster><lastBuildDate>Sat, 19 Sep 2026 21:33:52 GMT</lastBuildDate><ttl>20</ttl><item><title>Cannot close a form in the load event</title><link>http://forum.strataframe.net/FindPost28889.aspx</link><description>This error I am getting I dont think is a SF specific error but rather a dot net error. I have some validation I do the minute I load a form. If validation fails, I then want to close the form and exit. The code I have on the Load event&lt;br/&gt;&lt;br/&gt;if (validation = false)&lt;br/&gt;{this.close();}&lt;br/&gt;&lt;br/&gt;but I get a message :&amp;nbsp; Value Close() cannot be called while doing CreateHandle().&lt;br/&gt;&lt;br/&gt;Do I have this code in the right place ?</description><pubDate>Wed, 03 Nov 2010 15:35:31 GMT</pubDate><dc:creator>Ger Cannoll</dc:creator></item><item><title>RE: Cannot close a form in the load event</title><link>http://forum.strataframe.net/FindPost28899.aspx</link><description>I'm still trying to understand it myself&amp;nbsp; &lt;span&gt;:blink:&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;Here's what I've kind of figured out so far:&lt;br/&gt;&lt;br/&gt;- apparently forms load on a different thread (or something like that), so you can't just call a method like close during the load.&lt;br/&gt;- The BeginInvoke method is used to insert the close method into the message pump being used to open the form. &lt;br/&gt;- This has to do with window handles and how windows manages messages. I think what is going on is that besides the stuff we normally think about, like disposing of components and controls, the close method of a form also is releasing the windows handle....which may not actually be set in the load method.&amp;nbsp; That doesn't really make sense yet as the first thing the form does is create a handle, but maybe it hasn't actually registered the handle yet with windows.&amp;nbsp; Windows handles are used to manage messages (which get translated into events for us).&amp;nbsp; In any case BeginInvoke will actually search up the windows hierarchy until it finds a valid windows handle to work with, so it apparently avoids this issue.&lt;br/&gt;&lt;br/&gt;If that sounds kind of vague and confusing, well, it is.&amp;nbsp; Like I said I don't really understand how this works yet....</description><pubDate>Wed, 03 Nov 2010 15:35:31 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item><item><title>RE: Cannot close a form in the load event</title><link>http://forum.strataframe.net/FindPost28891.aspx</link><description>Hi Greg. Thaks for that and it woked fine. I had done a google search but could not find an answer as succinct as the one you located.&lt;br/&gt;&lt;br/&gt;Not sure though how it works ?. It uses a command :&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;br/&gt;&lt;br/&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size=2 face=Consolas&gt;&lt;font color=#000000 size=2 face=Consolas&gt;.BeginInvoke(&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;new&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size=2 face=Consolas&gt;&lt;font color=#000000 size=2 face=Consolas&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color=#2b91af size=2 face=Consolas&gt;&lt;font color=#2b91af size=2 face=Consolas&gt;&lt;font color=#2b91af size=2 face=Consolas&gt;MethodInvoker&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size=2 face=Consolas&gt;&lt;font color=#000000 size=2 face=Consolas&gt;(&lt;/font&gt;&lt;/font&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;&lt;font color=#0000ff size=2 face=Consolas&gt;this&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size=2 face=Consolas&gt;&lt;font size=2 face=Consolas&gt;&lt;font color=#000000&gt;.Close));&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;&lt;font size=2 face=Consolas&gt;&lt;font size=2 face=Consolas&gt;&lt;font color=#000000&gt;Why is something like this required.....must be something to do with the fact that the form is not actually loaded yet, even though its being run on the 'Load' event &lt;/font&gt;&lt;br/&gt;&lt;br/&gt;&lt;/font&gt;&lt;/font&gt;</description><pubDate>Tue, 02 Nov 2010 11:24:58 GMT</pubDate><dc:creator>Ger Cannoll</dc:creator></item><item><title>RE: Cannot close a form in the load event</title><link>http://forum.strataframe.net/FindPost28890.aspx</link><description>Found this on Google:&lt;br/&gt;&lt;br/&gt;&lt;a href="http://kiavashshakibaee.blogspot.com/2008/08/winforms-close-your-form-during-load.html"&gt;Winforms: Close your form during Load Event&lt;/a&gt;</description><pubDate>Tue, 02 Nov 2010 09:35:50 GMT</pubDate><dc:creator>Greg McGuffey</dc:creator></item></channel></rss>