<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'thread'</title><link>http://bloggingabout.net/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=thread&amp;orTags=0</link><description>Search results matching tag 'thread'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Hunting for window handles</title><link>http://bloggingabout.net/blogs/waseem/archive/2006/11/23/Hunting-for-window-handles.aspx</link><pubDate>Thu, 23 Nov 2006 01:08:52 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:54582</guid><dc:creator>Waseem Sadiq</dc:creator><description>&lt;p&gt;I am working on a fairy large WinForms project and after a certain build our application suddenly started crashing. Click, click, boem... gone.... no exception, no event log entry, nothing. The annoying part was that this didn't happened while executing&amp;nbsp;&lt;em&gt;inside&lt;/em&gt; the debugger, only when the testers were clicking around inside the application and even &lt;em&gt;then&lt;/em&gt; very undeterministically.&lt;/p&gt; &lt;p&gt;If you get problems like this the first thing to do is to attach a event handler to the Application.ThreadException event to try to figure out who is causing this error:&lt;/p&gt;&lt;pre class="csharpcode"&gt;Application.ThreadException += &lt;span class="kwrd"&gt;new&lt;/span&gt; ThreadExceptionEventHandler(Application_ThreadException);&lt;/pre&gt;
&lt;p&gt;Inside this event handler you can basically do diagnostics to find out what exception is being raised by looking at the ThreadExceptionEventArgs parameter.&lt;/p&gt;
&lt;p&gt;So in this case the exception was "&lt;em&gt;Error Creating Window Handle&lt;/em&gt;"... exactly the kind of exception that scares me. As Julia Lerman writes in a &lt;a href="http://blog.ziffdavis.com/devlife/archive/2006/02/22/40253.aspx"&gt;blog-post&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;”Error Creating Window Handle” is a&amp;nbsp;Win32 error, therefore the first thing I should have been looking at was any code that did anything with unmanaged items.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And it just so happened to be that one of the changes that we made to the application involved some GDI+ brushes and stuff like that. The first thing I did was making sure every brush and bitmap was disposed after being used by including them in the using pattern. Unfortunatly that alone didn't help much.&lt;/p&gt;
&lt;p&gt;The specific part where we were using GDI+ drawing was inside a renderer for a given tabcontrol from a third party product that we are using in this application. Our application also needs to clear this&amp;nbsp;tabcontrol and&amp;nbsp;re-create all the tabs&amp;nbsp;again quite often (for instance when the user navigates to a new area).&lt;/p&gt;
&lt;p&gt;My huntch was that somehow every time this tabcontrol was refilled again the old tabs were still floating around somehwere in memory and not being garbage collected, it turned out that I was right... after changing the code from simply doing a:&lt;/p&gt;&lt;pre class="csharpcode"&gt;leftTabControl.TabPages.Clear();&lt;/pre&gt;
&lt;p&gt;...to removing and disposing each individual item...&lt;/p&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;foreach&lt;/span&gt; (TabPage tab &lt;span class="kwrd"&gt;in&lt;/span&gt; leftTabControl.TabPages)
{
    leftTabControl.TabPages.Remove(tab);
    tab.Dispose();
}&lt;/pre&gt;
&lt;p&gt;...the problem went away.&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }


.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</description></item></channel></rss>