<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Simon Ottenhaus &#187; Win32Exception</title>
	<atom:link href="http://familie-ottenhaus.de/simon/blog/tag/win32exception/feed/" rel="self" type="application/rss+xml" />
	<link>http://familie-ottenhaus.de/simon/blog</link>
	<description>Uni &#38; Software</description>
	<lastBuildDate>Fri, 23 Dec 2011 13:15:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>c#: Win32Exception: Fehler beim Erstellen des Fensterhandles</title>
		<link>http://familie-ottenhaus.de/simon/blog/2009/12/csharp-win32exception-fehler-beim-erstellen-des-fensterhandles/</link>
		<comments>http://familie-ottenhaus.de/simon/blog/2009/12/csharp-win32exception-fehler-beim-erstellen-des-fensterhandles/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 16:21:01 +0000</pubDate>
		<dc:creator>Simon Ottenhaus</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Win32Exception]]></category>

		<guid isPermaLink="false">http://familie-ottenhaus.de/simon/blog/?p=973</guid>
		<description><![CDATA[Wann tritt diese Exception auf? &#8211; Wenn zu viele Steuerelemente erstellt und nicht wieder aufgeräumt wurden. Beispiel View Code CSHARP1 2 3 4 5 6 7 8 9 10 11 12 public partial class Form1 : Form &#123; void ForceException&#40;&#41; &#123; while &#40;true&#41; &#123; Label l = new Label&#40;&#41;; this.Controls.Add&#40;l&#41;; this.Controls.Clear&#40;&#41;; &#125; &#125; &#125; Bei [...]]]></description>
			<content:encoded><![CDATA[<p>Wann tritt diese Exception auf? &#8211; Wenn zu viele Steuerelemente erstellt und nicht wieder aufgeräumt wurden.</p>
<p><strong>Beispiel</strong></p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p973code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p9733"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p973code3"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Form1 <span style="color: #008000;">:</span> Form
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">void</span> ForceException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Label l <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Label<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>l<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Bei jedem Schleifendurchlauf wird ein neues Label erstellt. mit this.Controls.Clear(); wird das Label zwar entfernt, aber das Fenster-Handle des Labels existiert weiterhin. Da jeder Prozess nur begrenzt viele Fenster-Handles haben kann sind diese irgendwann aufgebraucht.</p>
<p><strong>Abhilfe</strong></p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p973code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p9734"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p973code4"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Form1 <span style="color: #008000;">:</span> Form
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">void</span> ForceException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                Label l <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Label<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>l<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Controls</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                l.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Mit l.Dispose(); werden alle Ressourcen, die das Label reserviert hatte freigegeben. Damit wird auch das Fenster-Handle wieder frei und kann später wieder verwendet werden. Die obige while-Schleife läuft beliebig lange ohne Fehler.</p>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://familie-ottenhaus.de/simon/blog/2009/12/csharp-win32exception-fehler-beim-erstellen-des-fensterhandles/" target="_blank" class="liimagelink"><img src="http://familie-ottenhaus.de/simon/blog/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://familie-ottenhaus.de/simon/blog/2009/12/csharp-win32exception-fehler-beim-erstellen-des-fensterhandles/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

