<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Close All Forms in an Application in a Thread-Safe Manner</title>
	<atom:link href="http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/</link>
	<description>C# Information, Code, Tips and News</description>
	<lastBuildDate>Sat, 24 Jul 2010 01:26:51 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Close All Forms in an Application in a Thread-Safe Manner &#124; .Net 2.0 Tips</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-5416</link>
		<dc:creator>Close All Forms in an Application in a Thread-Safe Manner &#124; .Net 2.0 Tips</dc:creator>
		<pubDate>Sat, 03 Apr 2010 05:58:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-5416</guid>
		<description>[...] Second, it’s quite possible that your application opened one or more forms in a separate thread.  If so, then trying to close a form from your main thread will throw an exception.  So you need to close your forms in a thread-safe manner:   view source [...]</description>
		<content:encoded><![CDATA[<p>[...] Second, it’s quite possible that your application opened one or more forms in a separate thread.  If so, then trying to close a form from your main thread will throw an exception.  So you need to close your forms in a thread-safe manner:   view source [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tejaswini</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-5415</link>
		<dc:creator>Tejaswini</dc:creator>
		<pubDate>Sat, 03 Apr 2010 05:47:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-5415</guid>
		<description>Nice article... n learn a nice technique.</description>
		<content:encoded><![CDATA[<p>Nice article&#8230; n learn a nice technique.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jason K. Carter</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-5413</link>
		<dc:creator>Jason K. Carter</dc:creator>
		<pubDate>Wed, 24 Mar 2010 12:51:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-5413</guid>
		<description>I don&#039;t think this solution is thread-safe either.  Think about what happens if Application.OpenForms is modified by another thread opening or closing a form, while the static OpenForms property&#039;s get accessor is iterating through the foreach statement?  The OpenForms collection would be altered between iterations of the foreach, which causes a runtime exception.  Similar problems plague a simple for statement.

If you want true thread safety, then the OpenForms property will need to be wrapped in a lock(Application.OpenForms) statement.  Any other places in your application that open or close a form will likewise need to lock the same object.</description>
		<content:encoded><![CDATA[<p>I don't think this solution is thread-safe either.  Think about what happens if Application.OpenForms is modified by another thread opening or closing a form, while the static OpenForms property's get accessor is iterating through the foreach statement?  The OpenForms collection would be altered between iterations of the foreach, which causes a runtime exception.  Similar problems plague a simple for statement.</p>
<p>If you want true thread safety, then the OpenForms property will need to be wrapped in a lock(Application.OpenForms) statement.  Any other places in your application that open or close a form will likewise need to lock the same object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sunny</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-5380</link>
		<dc:creator>Sunny</dc:creator>
		<pubDate>Wed, 10 Feb 2010 10:46:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-5380</guid>
		<description>I Guess this code is very simple compare to the above solutions.

public static void CloseAllForms()
{
//Create a Collection to Store all Opened Forms. 
List formsList = new List(); 
//All all opened forms into a Collection. 
foreach (Form frm in Application.OpenForms) 
{
//Execulde the Current Form. 
if (frm.Name == &quot;Form1&quot;) 
continue; 
else 
formsList.Add(frm);
}
//Now Close the forms 
foreach (Form frm in formsList) 
{
frm.Close();
}
}</description>
		<content:encoded><![CDATA[<p>I Guess this code is very simple compare to the above solutions.</p>
<p>public static void CloseAllForms()<br />
{<br />
//Create a Collection to Store all Opened Forms.<br />
List formsList = new List();<br />
//All all opened forms into a Collection.<br />
foreach (Form frm in Application.OpenForms)<br />
{<br />
//Execulde the Current Form.<br />
if (frm.Name == "Form1&#8243;)<br />
continue;<br />
else<br />
formsList.Add(frm);<br />
}<br />
//Now Close the forms<br />
foreach (Form frm in formsList)<br />
{<br />
frm.Close();<br />
}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nina</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-5359</link>
		<dc:creator>Nina</dc:creator>
		<pubDate>Mon, 11 Jan 2010 11:58:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-5359</guid>
		<description>Thanks, i was having trouble closing a form created on a separate thread and this has solved it!</description>
		<content:encoded><![CDATA[<p>Thanks, i was having trouble closing a form created on a separate thread and this has solved it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: timm</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-4596</link>
		<dc:creator>timm</dc:creator>
		<pubDate>Tue, 12 May 2009 15:03:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-4596</guid>
		<description>Ryan, if you read the article, that doesn&#039;t work because closing the form modifies the collection, which then throws an exception while iterating.</description>
		<content:encoded><![CDATA[<p>Ryan, if you read the article, that doesn't work because closing the form modifies the collection, which then throws an exception while iterating.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-4595</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Tue, 12 May 2009 14:48:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-4595</guid>
		<description>its a lot more simple to just use the FormCollection, since that is what Application.OpenForms uses


FormCollection forms = Application.OpenForms;
                for( int i = 0; i &lt; forms.Count; i++ ) {
                    forms[i].Close();
                }</description>
		<content:encoded><![CDATA[<p>its a lot more simple to just use the FormCollection, since that is what Application.OpenForms uses</p>
<p>FormCollection forms = Application.OpenForms;<br />
                for( int i = 0; i &lt; forms.Count; i++ ) {<br />
                    forms[i].Close();<br />
                }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: timm</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-4525</link>
		<dc:creator>timm</dc:creator>
		<pubDate>Sun, 05 Apr 2009 18:16:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-4525</guid>
		<description>Yes, I see how that can be confusing.  OpenForms in the second example is a static method that I wrote which returns an array of open forms (versus Application.OpenForms which returns a collection).  Download the example, but here is the code:

public static Form[] OpenForms
{
	get
	{
		Form[] forms = null;
		int count = Application.OpenForms.Count;
		forms = new Form[count];
		if (count &gt; 0)
		{
			int index = 0;
			foreach (Form form in Application.OpenForms)
			{
				forms[index++] = form;
			}
		}
		return forms;
	}
}</description>
		<content:encoded><![CDATA[<p>Yes, I see how that can be confusing.  OpenForms in the second example is a static method that I wrote which returns an array of open forms (versus Application.OpenForms which returns a collection).  Download the example, but here is the code:</p>
<p>public static Form[] OpenForms<br />
{<br />
	get<br />
	{<br />
		Form[] forms = null;<br />
		int count = Application.OpenForms.Count;<br />
		forms = new Form[count];<br />
		if (count > 0)<br />
		{<br />
			int index = 0;<br />
			foreach (Form form in Application.OpenForms)<br />
			{<br />
				forms[index++] = form;<br />
			}<br />
		}<br />
		return forms;<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yen Fox</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-4524</link>
		<dc:creator>Yen Fox</dc:creator>
		<pubDate>Sun, 05 Apr 2009 16:04:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-4524</guid>
		<description>Sound good but this statement doesn&#039;t pass the compiler
Form[] forms = OpenForms;
I get an error said can not implicitly convert Applicaton form collection to System.Windows.Forms</description>
		<content:encoded><![CDATA[<p>Sound good but this statement doesn't pass the compiler<br />
Form[] forms = OpenForms;<br />
I get an error said can not implicitly convert Applicaton form collection to System.Windows.Forms</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Visual C# Kicks</title>
		<link>http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/comment-page-1/#comment-4286</link>
		<dc:creator>Visual C# Kicks</dc:creator>
		<pubDate>Thu, 12 Mar 2009 03:15:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.csharp411.com/close-all-forms-in-a-thread-safe-manner/#comment-4286</guid>
		<description>Learned something new!</description>
		<content:encoded><![CDATA[<p>Learned something new!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
