<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>Buz &amp; Skup's .Net C# (C Sharp) Blog</title><link>http://www.pixvillage.com/blogs/devblog/</link><description>Buz &amp; Skup's .Net C# (C Sharp) Blog</description><managingEditor>Buz &amp; Skup</managingEditor><dc:language>en-US</dc:language><generator>.Text Version 0.95.2004.102</generator><item><dc:creator>Buz &amp; Skup</dc:creator><title>Moving !</title><link>http://www.pixvillage.com/blogs/devblog/archive/2008/11/21/6581.aspx</link><pubDate>Fri, 21 Nov 2008 15:05:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2008/11/21/6581.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6581.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2008/11/21/6581.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6581.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6581.aspx</trackback:ping><description>After all that time I've moved to&lt;br/&gt;
&lt;a href="http://www.thinkbeforecoding.com"&gt;www.thinkbeforecoding.com&lt;/a&gt;&lt;br/&gt;

The content here will remain available, so don't worry !

Skup&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6581.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>Downloading a File with a Save As Dialog in ASP.Net</title><link>http://www.pixvillage.com/blogs/devblog/archive/2007/05/24/6496.aspx</link><pubDate>Thu, 24 May 2007 11:49:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2007/05/24/6496.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6496.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2007/05/24/6496.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6496.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6496.aspx</trackback:ping><description>&lt;P&gt;Scott Guthrie &lt;A href="http://weblogs.asp.net/scottgu/archive/2007/05/22/may-22nd-link-listing-asp-net-visual-studio-silverlight-wpf-and-net.aspx"&gt;points&lt;/A&gt;&amp;nbsp;to&amp;nbsp;&lt;A href="http://west-wind.com/weblog/posts/76293.aspx"&gt;Rick Strahl's&amp;nbsp;blog&lt;/A&gt; about how to automaticaly open a Save As dialog on a link click. Very useful to download attachments.&lt;/P&gt;
&lt;P&gt;I'd just like to add that IE and FireFox have different handling with non ASCII characters that we can use in my french country for instance... IE doesn't like those chars, so you have to URLEncode it. But when URLEncoded, FireFox doesn't catch it, and keeps '+' chars&amp;nbsp;instead of spaces...&lt;/P&gt;
&lt;P&gt;The solution is simple.. you just have to check the client browser :&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: #e0e0e0 1px solid; BORDER-TOP: #e0e0e0 1px solid; FONT-SIZE: 10pt; BACKGROUND: white; BORDER-LEFT: #e0e0e0 1px solid; COLOR: black; BORDER-BOTTOM: #e0e0e0 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; PrepareAttachmentReponse(&lt;SPAN style="COLOR: #2b91af"&gt;HttpContext&lt;/SPAN&gt; context, &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; filename)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (context.Request.Browser.Browser == &lt;SPAN style="COLOR: #a31515"&gt;"IE"&lt;/SPAN&gt;)&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; filename = context.Server.UrlPathEncode(filename);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// send response&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; context.Response.AppendHeader(&lt;SPAN style="COLOR: #a31515"&gt;"Content-Disposition"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;"attachment; filename=\""&lt;/SPAN&gt; + filename + &lt;SPAN style="COLOR: #a31515"&gt;"\""&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;And that's it.&lt;!--EndFragment--&gt;&lt;/P&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6496.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>Disable Screen Saver while running your WPF Application</title><link>http://www.pixvillage.com/blogs/devblog/archive/2007/02/27/6493.aspx</link><pubDate>Tue, 27 Feb 2007 23:46:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2007/02/27/6493.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6493.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2007/02/27/6493.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6493.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6493.aspx</trackback:ping><description>&lt;P&gt;I'm working on a WFP Media player application, and the first time I used it to watch a video, I just discovered I had forgotten something critical when the screen went black and a 3D stylish 'Woosh' apperead : that f***ing screen saver ! &lt;/P&gt;
&lt;P&gt;But how to disable it ?!&lt;BR&gt;First I searched the Internet and found something to disable it in the registry. But I don't like this, you need rights to write the registry, and how can you turn the screen saver back if your application quit unexpectedly (Ok you'll say it won't, it's well written, but I can kill it with the task manager :-S ). No way. &lt;/P&gt;
&lt;P&gt;Then I found something about the &lt;CODE&gt;WM_SYSCOMMAND message&lt;/CODE&gt;. The window receives this message with a value of &lt;CODE&gt;SC_SCREENSAVE&lt;/CODE&gt; when the screen saver should appear and you can prevent it to run by handling the message without doing anything. You can also get the &lt;CODE&gt;SC_MONITORPOWER&lt;/CODE&gt; value when the monitor enters a low consumption state. &lt;/P&gt;
&lt;P&gt;I like it ! But the point is how to handle this Win32 message in WPF, the no-windows-message world ? &lt;/P&gt;
&lt;P&gt;Actually, the main WPF Window is hosted in a Win32 window, represented by a &lt;CODE&gt;HwndSource&lt;/CODE&gt; object. You can get this object using the &lt;CODE&gt;PresentationSource.FromVisual&lt;/CODE&gt; method. The &lt;CODE&gt;HwndSource&lt;/CODE&gt; the provides a &lt;CODE&gt;AddHook&lt;/CODE&gt; method that takes a &lt;CODE&gt;Hook&lt;/CODE&gt; delegate that you can use to handle the message. It's a bit too easy. &lt;/P&gt;
&lt;P&gt;There is just one last thing. Calling &lt;CODE&gt;PresentationSource.FromVisual&lt;/CODE&gt; from the &lt;CODE&gt;Window&lt;/CODE&gt; constructor will return null since the window initialization is not terminated and it's not already hosted by the Win32 host. Just call it from the &lt;CODE&gt;SourceInitialized&lt;/CODE&gt; event. &lt;/P&gt;
&lt;P&gt;A minimal sample to summary all we said :&lt;/P&gt;

&lt;div class="code" style="font-family: Courier New; font-size: 10pt; color: black; background: white; border: solid 1p #e0e0e0;"&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;partial&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;MainWindow&lt;/span&gt; : &lt;span style="color: #2b91af;"&gt;Window&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; WM_SYSCOMMAND = 0x112;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; SC_SCREENSAVE = 0xF140;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;const&lt;/span&gt; &lt;span style="color: blue;"&gt;int&lt;/span&gt; SC_MONITORPOWER = 0xF170;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;public&lt;/span&gt; MainWindow()&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; SourceInitialized += &lt;span style="color: blue;"&gt;delegate&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;HwndSource&lt;/span&gt; source = (&lt;span style="color: #2b91af;"&gt;HwndSource&lt;/span&gt;)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af;"&gt;PresentationSource&lt;/span&gt;.FromVisual(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; source.AddHook(Hook);&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt; Hook(&lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt; hwnd,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;int&lt;/span&gt; msg, &lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt; wParam, &lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt; lParam,&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;ref&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; handled)&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (msg == WM_SYSCOMMAND &amp;amp;&amp;amp;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ((((&lt;span style="color: blue;"&gt;long&lt;/span&gt;)wParam &amp;amp; 0xFFF0) == SC_SCREENSAVE) ||&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;span style="color: blue;"&gt;long&lt;/span&gt;)wParam &amp;amp; 0xFFF0) == SC_MONITORPOWER))&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; handled = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;IntPtr&lt;/span&gt;.Zero;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/div&gt;
&lt;P&gt;Just notice the &lt;CODE&gt;0xFFF0&lt;/CODE&gt; mask, it's because the system can use the 4 low order bits of the wParam value as stated in the MSDN library article about &lt;CODE&gt;WM_SYSCOMMAND&lt;/CODE&gt;. &lt;/P&gt;
&lt;P&gt;Now I can watch videos without mooving the mouse every 10 minutes ! I hope it can make your apps better to ! &lt;/P&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6493.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>FireFox 2.0 clumsy iterators</title><link>http://www.pixvillage.com/blogs/devblog/archive/2006/10/24/6460.aspx</link><pubDate>Tue, 24 Oct 2006 16:57:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2006/10/24/6460.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6460.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2006/10/24/6460.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6460.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6460.aspx</trackback:ping><description>&lt;P&gt;Has everyone already nows, FireFox 2.0 is shipping this afternoon. It seems to be a nice release, and I'll surely use it often as soon as it's accessible.&lt;/P&gt;
&lt;P&gt;I was looking at the new javascript 1.7 features. Especially the iterator thing, that I use a lot in C# since .Net 2.0 and that will be used even further with C# 3.0.&lt;/P&gt;
&lt;P&gt;I was really sad to see how poorly it has been designed in javascript 1.7.&lt;BR&gt;It's ok to move to next item in the &lt;CODE&gt;next()&lt;/CODE&gt; method. &lt;B&gt;But why did they not provide a way to test if there is more items in the iterator ???&lt;/B&gt;&lt;/P&gt;
&lt;P&gt;When enumerating an iterator you have write something like this :&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: lightgrey 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: lightgrey 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 10pt; BACKGROUND: white; PADDING-BOTTOM: 4px; BORDER-LEFT: lightgrey 1px solid; COLOR: black; PADDING-TOP: 4px; BORDER-BOTTOM: lightgrey 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var it = Iterator(obj);&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;try&lt;/SPAN&gt; {&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;while&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;) {&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; document.write(it.next() + &lt;SPAN style="COLOR: maroon"&gt;"\n"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;SPAN style="COLOR: blue"&gt;catch&lt;/SPAN&gt; (err &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; err instanceof StopIteration) {&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; document.write(&lt;SPAN style="COLOR: maroon"&gt;"End of record.\n"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;SPAN style="COLOR: blue"&gt;catch&lt;/SPAN&gt; (err) {&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; document.write(&lt;SPAN style="COLOR: maroon"&gt;"Unknown error: "&lt;/SPAN&gt; + err.description + &lt;SPAN style="COLOR: maroon"&gt;"\n"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;There are two ugly things about that : 
&lt;UL&gt;
&lt;LI&gt;having to write &lt;CODE&gt;while(true)&lt;/CODE&gt; which should be something rather rare. 
&lt;LI&gt;having to catch an exception for a case that is really not exceptionnal ! You should never use exceptions in your main control flow !&lt;/LI&gt;&lt;/UL&gt;Why did they not add a &lt;CODE&gt;moreItems&lt;/CODE&gt; or any other name property to know if you can call &lt;CODE&gt;next()&lt;/CODE&gt;. You would write : 
&lt;P&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: lightgrey 1px solid; PADDING-RIGHT: 4px; BORDER-TOP: lightgrey 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 10pt; BACKGROUND: white; PADDING-BOTTOM: 4px; BORDER-LEFT: lightgrey 1px solid; COLOR: black; PADDING-TOP: 4px; BORDER-BOTTOM: lightgrey 1px solid; FONT-FAMILY: Consolas"&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; var it = Iterator(obj);&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;while&lt;/SPAN&gt; (it.moreItems) {&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; document.write(it.next() + &lt;SPAN style="COLOR: maroon"&gt;"\n"&lt;/SPAN&gt;);&lt;/P&gt;
&lt;P style="MARGIN: 0px; LINE-HEIGHT: 14px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; }&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;And it would be sooooooo clean ! &lt;/P&gt;
&lt;P&gt;But they'll release it, and any poor web developper will have to deal with this stupidity till FireFox 2.0 becomes obsolete in something like 6-7 years... F...k ! &lt;/P&gt;
&lt;P&gt;Skup&lt;/P&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6460.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>Readers, Writers and IDisposable</title><link>http://www.pixvillage.com/blogs/devblog/archive/2006/10/17/6450.aspx</link><pubDate>Tue, 17 Oct 2006 18:19:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2006/10/17/6450.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6450.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2006/10/17/6450.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6450.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6450.aspx</trackback:ping><description>&lt;p&gt; You know what Readers and Writers are... Wrappers around Streams used to write or read data.&lt;br /&gt;
Usually, you make things like this:
&lt;/p&gt;
&lt;div style="font-family: Consolas; font-size: 10pt; color: black; background: white; border: solid 1px lightgrey; padding:4px;"&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: teal;"&gt;Stream&lt;/span&gt; stream = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: teal;"&gt;BinaryWriter&lt;/span&gt; writer = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;BinaryWriter&lt;/span&gt;(stream))&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(10);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(20);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
You create your stream and your writer, use it and dispose both. But what if you mustn't dispose the stream?
&lt;/p&gt;
&lt;div style="font-family: Consolas; font-size: 10pt; color: black; background: white; border: solid 1px lightgrey; padding:4px;"&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Method()&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: teal;"&gt;Stream&lt;/span&gt; stream = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; WriteToStream(stream);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; stream.Position = 0;&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// .. Read stream&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; WriteToStream(&lt;span style="color: teal;"&gt;Stream&lt;/span&gt; stream)&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: teal;"&gt;BinaryWriter&lt;/span&gt; writer = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;BinaryWriter&lt;/span&gt;(stream))&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(10);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(20);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// !! Dispose the writer and the underlying stream!&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The implicit call to &lt;code&gt;Dispose&lt;/code&gt; on the writer at the end of the &lt;code&gt;using&lt;/code&gt; block will dispose the writer and
the underlying stream... Then you cannot read the stream anymore.
&lt;/p&gt;
&lt;p&gt;But since the writer is &lt;code&gt;IDisposable&lt;/code&gt;, shouldn't I call &lt;code&gt;Dispose&lt;/code&gt; on it ? And - more important -
won't it dispose the stream at random time when the finalizer is called?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The response is No, and No.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Actually, the Readers and Writers are &lt;code&gt;IDisposable&lt;/code&gt; so that you can manage the underlying stream lifetime
without having to keep a reference on both objects. If you manually manage your stream lifetime, you don't have to
dispose your reader or writer.&lt;/p&gt;
&lt;p&gt;And what about the finalizer? It's never safe to use references to other managed objects in the finalizer since they
can have been finalized before. Finalizers only release the unmanaged resources. So the writer won't dispose the stream
when finalized. The writers and readers often don't directly reference unmanaged resources, and they don't even need
a finalizer.&lt;/p&gt;
&lt;p&gt;Here is the good way to write code above:&lt;/p&gt;
&lt;div style="font-family: Consolas; font-size: 10pt; color: black; background: white; border: solid 1px lightgrey; padding:4px;"&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; WriteToStream(&lt;span style="color: teal;"&gt;Stream&lt;/span&gt; stream)&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal;"&gt;BinaryWriter&lt;/span&gt; writer = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;BinaryWriter&lt;/span&gt;(stream);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(10);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; writer.Write(20);&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green;"&gt;// Just let the stream live&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px; line-height:14px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Skup
&lt;/p&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6450.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>A good bug is a dead bug</title><link>http://www.pixvillage.com/blogs/devblog/archive/2006/10/13/6436.aspx</link><pubDate>Fri, 13 Oct 2006 10:53:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2006/10/13/6436.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6436.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2006/10/13/6436.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6436.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6436.aspx</trackback:ping><description>&lt;P&gt;I agree with Lieutenant Jean Rasczak: "The only good bug is a dead bug"...&lt;/P&gt;
&lt;P&gt;This one was in the PixVillage database engine core and was teasing me for more than one month ! I had not detected it despite the 97% code coverage and the eavy unit tests. Of course it appeared in a tested condition that gave a bad result in a special data case. What can you do against that ? &lt;/P&gt;
&lt;P&gt;Hopefully, once this kind of bug is spotted it's already almost dead...&lt;/P&gt;
&lt;P&gt;Skup&lt;/P&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6436.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>About event hooking</title><link>http://www.pixvillage.com/blogs/devblog/archive/2006/10/03/6325.aspx</link><pubDate>Tue, 03 Oct 2006 16:44:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2006/10/03/6325.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/6325.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2006/10/03/6325.aspx#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/6325.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/6325.aspx</trackback:ping><description>&lt;p&gt;Last week I was working on this really useful feature that is &lt;code&gt;MembershipProvider&lt;/code&gt;. I had
to write my own implementation since I'm working on a custom database schema. I began to write my own
provider, and decided to test it.
&lt;/p&gt;
&lt;p&gt;Easy, I put a &lt;code&gt;Login&lt;/code&gt; control on my login page, set some properties,
and launch the web site to see if it's working. Ok, it cannot work the first time, so I made little changes.
Among them, I hooked a method to the &lt;code&gt;Authenticate&lt;/code&gt; event to check if it
at least passed through it.&lt;/p&gt;
&lt;p&gt;
It was executing it, but it still wouldn't execute any code in my provider!?&lt;/p&gt;
&lt;p&gt;
I continued to make some changes in config. It should work, but nothing.&lt;/p&gt;
&lt;p&gt;
Guess what ! When you hook the &lt;code&gt;Authenticate&lt;/code&gt; event, the &lt;code&gt;Login&lt;/code&gt; control
stops doing its default behavior - that is authenticating the user with the specified Membership provider -
and let you do all the work yourself ! That's why nothing happened, and I spent one day and a half on this!&lt;/p&gt;
&lt;p&gt;This means that the &lt;code&gt;OnAuthenticate&lt;/code&gt; methods that raise the events should look something like this:&lt;/p&gt;
&lt;div class="code" style="font-family: Consolas; font-size: 10pt; color: black; background: white; border: solid 1px lightgrey; padding:4px;"&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; OnAuthenticate(&lt;span style="color: teal;"&gt;AuthenticateEventArgs&lt;/span&gt; e)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;if&lt;/span&gt; (Authenticate != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Authenticate(&lt;span style="color: blue;"&gt;this&lt;/span&gt;, e);&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; e.Authenticated;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue;"&gt;return&lt;/span&gt; DefaultAuthenticate();&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;It seams a bit error prone to me... what about adding a boolean &lt;code&gt;Handled&lt;/code&gt; property to the
&lt;code&gt;EventArg&lt;/code&gt; class to say that you did it on your own? Changing the behavior of a component
just because you hooked an event is a good way to drive developpers crazy...
&lt;/p&gt;
&lt;p&gt;Do you know other events with this behavior in the framework?
&lt;/p&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/6325.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>Events, Multithreading and Deadlocks</title><link>http://www.pixvillage.com/blogs/devblog/archive/2006/06/09/4869.aspx</link><pubDate>Fri, 09 Jun 2006 14:05:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2006/06/09/4869.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/4869.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2006/06/09/4869.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/4869.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/4869.aspx</trackback:ping><description>&lt;p &gt;I made a &lt;a href="http://msdnwiki.microsoft.com/en-us/mtpswiki/ms366768(vs.80).aspx"&gt;post&lt;/a&gt; today about this sad story on the new &lt;a href="http://msdnwiki.microsoft.com/"&gt;MSDN Wiki&lt;/a&gt;...&lt;/P&gt;
&lt;p&gt;The problem occures when using events in a multithreaded program.&lt;br /&gt;
You application get stuck from time to time and when you break execution with you debugger,
you find it stopped on a line like this :&lt;br /&gt;
&lt;span class="code"&gt;
AnObject.AnEvent -= &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; EventHandler(Event_Hanlder);
&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;
&lt;i&gt;Wtf ?! Why is this line blocking ?! This is just a -= operator, it cannot block execution !!&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
So lets take a look at how events are implemented.&lt;br /&gt;
Events manage internaly a list of delegates, and protect this list against concurrent access.
Thus, the list is locked in two main cases :
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;When raising an event. The event list is locked until returning from the event handler.
This prevent the list from changing while enumerating through the handlers.
&lt;/li&gt;
&lt;li&gt;When subscribing or unsubscribing from an event. The prevents the list to be modified
from different places.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
It's clearer now why deadlocks can occure.&lt;br /&gt;
When unsubscribing from an event EventB in an event handler called through EventA,
your thread holds two locks, first one on EventA, then one on EventB.&lt;br /&gt;
If another thread does the contrary (unsubscribe from EventA
in EventB), then it will first lock EventB, then try to lock EventA.&lt;br /&gt;&lt;br /&gt;
Obviously if the second thead has already locked EventB before the first thread does, the first
thread will block on the unsubscription from EventB until the second thread leaves the event handler. But since
the second thread will also block on the unsubscription from EventA until the first thread leaves the event handler,
a nasty deadlock occures.&lt;br /&gt;
You can refere to the &lt;a href="http://msdnwiki.microsoft.com/en-us/mtpswiki/ms366768(vs.80).aspx"&gt;sample code in the post on the MSDN Wiki&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
It is not realy kind of event implementers to have hidden locks in the code.
There would be a good way to remove one of the two lock reasons : instead of locking
the event while calling event handlers, it would be possible to lock the event while making
a copy of the handlers, then call the event handlers from the list copy. This way the list cannot
be modified during the call, and there is no risk of reentrant calls.&lt;br /&gt;
The other solution is to unsubscribe the events out the the event handler thread by
launching new threads to perform that task, but it makes the execution far less predictive...
&lt;/p&gt;
&lt;p&gt;If you have better solution, just tell me !&lt;/p&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/4869.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>RSS for Asp.Net 2.0</title><link>http://www.pixvillage.com/blogs/devblog/archive/2006/02/28/3595.aspx</link><pubDate>Tue, 28 Feb 2006 16:35:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2006/02/28/3595.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/3595.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2006/02/28/3595.aspx#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/3595.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/3595.aspx</trackback:ping><description>&lt;P&gt;Dmitryr has released a nice &lt;A href="http://blogs.msdn.com/dmitryr/archive/2006/02/21/536552.aspx"&gt;RSS Toolkit for ASP.NET 2.0&lt;/A&gt;&amp;nbsp;that includes a RSS data source component that enables databinding of rss feeds on any asp.net control, and a full support for RSS feeds&amp;nbsp;generation.&lt;/P&gt;
&lt;P&gt;You can also refere to &lt;A href="http://weblogs.asp.net/scottgu/archive/2006/02/22/438738.aspx"&gt;scott gutrie's blog&lt;/A&gt; for a brief tutorial.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Enjoy&lt;/P&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/3595.aspx" width = "1" height = "1" /&gt;</description></item><item><dc:creator>Buz &amp; Skup</dc:creator><title>Single Instance Application - Part 2 : activating existing instance</title><link>http://www.pixvillage.com/blogs/devblog/archive/2005/11/24/3030.aspx</link><pubDate>Thu, 24 Nov 2005 18:00:00 GMT</pubDate><guid>http://www.pixvillage.com/blogs/devblog/archive/2005/11/24/3030.aspx</guid><wfw:comment>http://www.pixvillage.com/blogs/devblog/comments/3030.aspx</wfw:comment><comments>http://www.pixvillage.com/blogs/devblog/archive/2005/11/24/3030.aspx#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.pixvillage.com/blogs/devblog/comments/commentRss/3030.aspx</wfw:commentRss><trackback:ping>http://www.pixvillage.com/blogs/devblog/services/trackbacks/3030.aspx</trackback:ping><description>  &lt;p&gt;Well, I did not have post for a long time. I was working hard on the next version of
   &lt;a href="http://www.pixvillage.com"&gt;PixVillage&lt;/a&gt;, which will be released soon now.&lt;/p&gt;
  
  &lt;p&gt;I'm coming back with a new post in the &lt;i&gt;Single Instance Application&lt;/i&gt; series.&lt;/p&gt;
  &lt;p&gt;In the previous post, we have written a &lt;i&gt;ProcessLock&lt;/i&gt; object that help us to detect
   an existing instance of the program we attempt to run, so that we can prevent running it twice.&lt;/p&gt;
  
  &lt;p&gt;
   Today I'm dealing with the ability to activate an existing instance of a &lt;i&gt;Windows Forms&lt;/i&gt;
   application when running a new one. In order to achieve that, we have to notify the application 
   it has to go foreground and get focus.&lt;/p&gt;
   
  &lt;p&gt;This can be done though a message send to the main thread of the application. 
   The message is received by the thread, not by a window, so we have to write a specific handler 
   and register it. The &lt;i&gt;IMessageFilter&lt;/i&gt; interface helps providing this kind of service. Let's implement it!&lt;/p&gt;
  
  &lt;!-- ThreadMessageFilter code --&gt;
  &lt;div style='margin-left:10pt;margin-right:2pt;border:#c0c0c0 1px solid'&gt;&lt;div style="font-size:10pt;border-bottom:#c0c0c0 1px solid" onclick="var i=this.sel,j;for(j=0;j&lt;3;j++)this.childNodes[2+j*2].checked=j==i;if(i==this.selold)return;var sh=new Array('none',''),o=this.parentNode;j=i==0?0:1;this.selold=i;o.childNodes[1].style.display=sh[j];o.childNodes[2].style.display=sh[1-j];o=o.childNodes[1];var h =o.offsetHeight;o.style.height=i==1?'200pt':'';if(i==1 &amp;&amp; o.offsetHeight&gt;h)o.style.height=h;" sel=1 selold=1&gt;&lt;b&gt;  Code &lt;/b&gt;&lt;a href='javascript:' onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');"&gt;  Copy  &lt;/a&gt;&lt;input type=Radio onclick="parentNode.sel=0" name="ef385928b09345ea82636027bf303909" ID="Radio1" VALUE="Radio1"&gt;Hide&lt;input type=Radio onclick="parentNode.sel=1" name="ef385928b09345ea82636027bf303909" checked ID="Radio2" VALUE="Radio2"&gt;Scroll&lt;input type=Radio onclick="parentNode.sel=2" name="ef385928b09345ea82636027bf303909" ID="Radio3" VALUE="Radio3"&gt;Full&lt;/div&gt;&lt;div style="overflow:auto;font-size:15;font-family:'Courier New';Height:200"&gt;&lt;font color=#0000FF&gt;namespace&lt;/font&gt; SingleInstance {&lt;div style="margin-left:20pt"&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;summary&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#008000&gt; Provides a thread message filter and handle messages.&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;/summary&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#0000FF&gt;public&lt;/font&gt;&lt;font color=#0000FF&gt; class&lt;/font&gt; ThreadMessageFilter : IMessageFilter {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt; Form owner;&lt;br&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;summary&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#008000&gt; Initializes a new instance of the&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;see cref=&amp;quot;ThreadMessageFilter&amp;quot;/&amp;gt;&lt;/font&gt;&lt;font color=#008000&gt; class.&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;/summary&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;param name=&amp;quot;owner&amp;quot;&amp;gt;&lt;/font&gt;&lt;font color=#008000&gt;The owner.&lt;/font&gt;&lt;font color=#808080&gt;&amp;lt;/param&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#0000FF&gt;public&lt;/font&gt; ThreadMessageFilter(Form owner) {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;this&lt;/font&gt;.owner = owner;&lt;/div&gt;}&lt;br&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;exclude/&amp;gt;&lt;/font&gt;&lt;br&gt;[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=&lt;font color=#0000FF&gt;true&lt;/font&gt;)]&lt;br&gt;&lt;font color=#0000FF&gt;bool&lt;/font&gt; IMessageFilter.PreFilterMessage(&lt;font color=#0000FF&gt;ref&lt;/font&gt; Message m) {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;if&lt;/font&gt; (m.HWnd != IntPtr.Zero)&lt;font color=#008000&gt; // Get rid of message if it's sent to a window...&lt;/font&gt;&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;return&lt;/font&gt;&lt;font color=#0000FF&gt; false&lt;/font&gt;;&lt;br&gt;&lt;font color=#008000&gt;&lt;/div&gt;// Handle the message here...&lt;br&gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#0000FF&gt;return&lt;/font&gt;&lt;font color=#0000FF&gt; false&lt;/font&gt;;&lt;/div&gt;}&lt;/div&gt;}&lt;/div&gt;}&lt;/div&gt;&lt;div style="display:none;background-color:#ffffff"&gt;&lt;b&gt; . . .&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;
  &lt;!-- ThreadMessageFilter code end --&gt;
  &lt;p&gt;Now, supposing we want to activate the window when a new instance of the application is launched, 
   we can, for instance, send a &lt;i&gt;WM_SHOWWINDOW&lt;/i&gt; message to the thread, using the Windows 
   &lt;i&gt;PostThreadMessage&lt;/i&gt; API. The &lt;i&gt;IMessageFilter.PreFilterMessage&lt;/i&gt; is implemented as follow:&lt;/p&gt;
   
  &lt;!-- WM_SHOWWINDOW handling code --&gt;
  &lt;div style='margin-left:10pt;margin-right:2pt;border:#c0c0c0 1px solid'&gt;&lt;div style="font-size:10pt;border-bottom:#c0c0c0 1px solid" onclick="var i=this.sel,j;for(j=0;j&lt;3;j++)this.childNodes[2+j*2].checked=j==i;if(i==this.selold)return;var sh=new Array('none',''),o=this.parentNode;j=i==0?0:1;this.selold=i;o.childNodes[1].style.display=sh[j];o.childNodes[2].style.display=sh[1-j];o=o.childNodes[1];var h =o.offsetHeight;o.style.height=i==1?'200pt':'';if(i==1 &amp;&amp; o.offsetHeight&gt;h)o.style.height=h;" sel=1 selold=1&gt;&lt;b&gt;  Code &lt;/b&gt;&lt;a href='javascript:' onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');"&gt;  Copy  &lt;/a&gt;&lt;input type=Radio onclick="parentNode.sel=0" name="6e09d35712bb49e391c0d707fddae175" ID="Radio4" VALUE="Radio4"&gt;Hide&lt;input type=Radio onclick="parentNode.sel=1" name="6e09d35712bb49e391c0d707fddae175" checked ID="Radio5" VALUE="Radio5"&gt;Scroll&lt;input type=Radio onclick="parentNode.sel=2" name="6e09d35712bb49e391c0d707fddae175" ID="Radio6" VALUE="Radio6"&gt;Full&lt;/div&gt;&lt;div style="overflow:auto;font-size:15;font-family:'Courier New';Height:200"&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;exclude/&amp;gt;&lt;/font&gt;&lt;br&gt;[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=&lt;font color=#0000FF&gt;true&lt;/font&gt;)]&lt;br&gt;&lt;font color=#0000FF&gt;bool&lt;/font&gt; IMessageFilter.PreFilterMessage(&lt;font color=#0000FF&gt;ref&lt;/font&gt; Message m) {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;if&lt;/font&gt; (m.HWnd != IntPtr.Zero)&lt;font color=#008000&gt; // Get rid of message if it's sent to a window...&lt;/font&gt;&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;return&lt;/font&gt;&lt;font color=#0000FF&gt; false&lt;/font&gt;;&lt;br&gt;&lt;font color=#0000FF&gt;&lt;/div&gt;if&lt;/font&gt; (m.Msg == WM_SHOWWINDOW) {&lt;div style="margin-left:20pt"&gt;&lt;font color=#008000&gt;// Shows the window&lt;/font&gt;&lt;br&gt;&lt;font color=#0000FF&gt;try&lt;/font&gt; {&lt;div style="margin-left:20pt"&gt;owner.Show();&lt;br&gt;&lt;font color=#0000FF&gt;if&lt;/font&gt; (owner.WindowState == FormWindowState.Minimized)&lt;div style="margin-left:20pt"&gt;owner.WindowState = FormWindowState.Normal;&lt;br&gt;&lt;font color=#0000FF&gt;&lt;/div&gt;return&lt;/font&gt;&lt;font color=#0000FF&gt; true&lt;/font&gt;;&lt;/div&gt;}&lt;br&gt;&lt;font color=#0000FF&gt;catch&lt;/font&gt; {}&lt;font color=#008000&gt; // return false;&lt;/font&gt;&lt;/div&gt;}&lt;br&gt;&lt;br&gt;&lt;font color=#0000FF&gt;return&lt;/font&gt;&lt;font color=#0000FF&gt; false&lt;/font&gt;;&lt;/div&gt;}&lt;br&gt;&lt;/div&gt;&lt;div style="display:none;background-color:#ffffff"&gt;&lt;b&gt; . . .&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;
  &lt;!-- WM_SHOWWINDOW handling code end --&gt;
  
  &lt;p&gt;With &lt;i&gt;WM_SHOWWINDOW&lt;/i&gt; declared as follow:&lt;/p&gt;
  
  &lt;!-- WM_SHOWWINDOW code --&gt;
  &lt;div style='margin-left:10pt;margin-right:2pt;border:#c0c0c0 1px solid'&gt;&lt;div style="font-size:10pt;border-bottom:#c0c0c0 1px solid"&gt;&lt;b&gt;  Code &lt;/b&gt;&lt;a href='javascript:' onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');"&gt;  Copy  &lt;/a&gt;&lt;/div&gt;&lt;div style="overflow:auto;font-size:15;font-family:'Courier New'"&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt;&lt;font color=#0000FF&gt; const&lt;/font&gt;&lt;font color=#0000FF&gt; int&lt;/font&gt; WM_SHOWWINDOW = 0x18;&lt;br&gt;&lt;/div&gt;&lt;/div&gt;  
  &lt;!-- WM_SHOWWINDOW code end --&gt;
  &lt;p&gt;Now, we just have to modify the &lt;i&gt;Main()&lt;/i&gt; method in order to:&lt;/p&gt;
  &lt;ul&gt;
   &lt;li&gt;Create and show the form the first time the application is started.&lt;/li&gt;
   &lt;li&gt;Registers and unregisters a &lt;i&gt;ThreadMessageFilter&lt;/i&gt; object.&lt;/li&gt;
   &lt;li&gt;Send the message to any thread of any process having the same name as the current assembly.&lt;/li&gt;
  &lt;/ul&gt;
  &lt;p&gt;The code is given below. (&lt;i&gt;MainForm&lt;/i&gt; is a class derived from &lt;i&gt;System.Windows.Form&lt;/i&gt;).&lt;/p&gt;
  &lt;!-- Main() code --&gt;
  &lt;div style='margin-left:10pt;margin-right:2pt;border:#c0c0c0 1px solid'&gt;&lt;div style="font-size:10pt;border-bottom:#c0c0c0 1px solid" onclick="var i=this.sel,j;for(j=0;j&lt;3;j++)this.childNodes[2+j*2].checked=j==i;if(i==this.selold)return;var sh=new Array('none',''),o=this.parentNode;j=i==0?0:1;this.selold=i;o.childNodes[1].style.display=sh[j];o.childNodes[2].style.display=sh[1-j];o=o.childNodes[1];var h =o.offsetHeight;o.style.height=i==1?'200pt':'';if(i==1 &amp;&amp; o.offsetHeight&gt;h)o.style.height=h;" sel=0 selold=0&gt;&lt;b&gt;  Code &lt;/b&gt;&lt;a href='javascript:' onclick="window.clipboardData.setData('Text',this.parentNode.parentNode.childNodes[1].innerText);alert('Code copied to clipboard');"&gt;  Copy  &lt;/a&gt;&lt;input type=Radio onclick="parentNode.sel=0" name="d66e1eda9d3d48aba90db3046ca2fffa" checked ID="Radio7" VALUE="Radio7"&gt;Hide&lt;input type=Radio onclick="parentNode.sel=1" name="d66e1eda9d3d48aba90db3046ca2fffa" ID="Radio8" VALUE="Radio8"&gt;Scroll&lt;input type=Radio onclick="parentNode.sel=2" name="d66e1eda9d3d48aba90db3046ca2fffa" ID="Radio9" VALUE="Radio9"&gt;Full&lt;/div&gt;&lt;div style="overflow:auto;font-size:15;font-family:'Courier New';display:'none'"&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt; System;&lt;br&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt; System.Diagnostics;&lt;br&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt; System.Reflection;&lt;br&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt; System.Runtime.InteropServices;&lt;br&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt; System.Windows.Forms;&lt;br&gt;&lt;br&gt;&lt;font color=#0000FF&gt;namespace&lt;/font&gt; SingleInstance&lt;br&gt;{&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;public&lt;/font&gt;&lt;font color=#0000FF&gt; sealed&lt;/font&gt;&lt;font color=#0000FF&gt; class&lt;/font&gt; SingleInstance&lt;br&gt;{&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt; SingleInstance(){}&lt;br&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt;&lt;font color=#0000FF&gt; static&lt;/font&gt;&lt;font color=#0000FF&gt; readonly&lt;/font&gt;&lt;font color=#0000FF&gt; string&lt;/font&gt; assemblyName = Assembly.GetExecutingAssembly().GetName().Name;&lt;br&gt;&lt;br&gt;[DllImport(&amp;quot;user32.dll&amp;quot;)]&lt;br&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt;&lt;font color=#0000FF&gt; static&lt;/font&gt;&lt;font color=#0000FF&gt; extern&lt;/font&gt;&lt;font color=#0000FF&gt; bool&lt;/font&gt; PostThreadMessage(&lt;font color=#0000FF&gt;int&lt;/font&gt; threadId,&lt;font color=#0000FF&gt; int&lt;/font&gt; message,&lt;font color=#0000FF&gt; int&lt;/font&gt; wParam,&lt;font color=#0000FF&gt; int&lt;/font&gt; lParam);&lt;br&gt;&lt;br&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt;&lt;font color=#0000FF&gt; const&lt;/font&gt;&lt;font color=#0000FF&gt; int&lt;/font&gt; WM_SHOWWINDOW = 0x18;&lt;br&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;summary&amp;gt;&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#008000&gt; Application entry point.&lt;/font&gt;&lt;br&gt;&lt;font color=#808080&gt;///&lt;/font&gt;&lt;font color=#808080&gt; &amp;lt;/summary&amp;gt;&lt;/font&gt;&lt;br&gt;[STAThread]&lt;br&gt;&lt;font color=#0000FF&gt;static&lt;/font&gt;&lt;font color=#0000FF&gt; void&lt;/font&gt; Main() {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt;(ProcessLock processLock =&lt;font color=#0000FF&gt; new&lt;/font&gt; ProcessLock(assemblyName)) {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;if&lt;/font&gt; (processLock.AlreadyExists)&lt;br&gt;{&lt;div style="margin-left:20pt"&gt;&lt;font color=#008000&gt;// Sets the existing application foreground.&lt;/font&gt;&lt;br&gt;SetForeground();&lt;/div&gt;}&lt;br&gt;&lt;font color=#0000FF&gt;else&lt;/font&gt;&lt;br&gt;{&lt;div style="margin-left:20pt"&gt;&lt;font color=#008000&gt;// The program operation must run inside the 'using' block.&lt;/font&gt;&lt;br&gt;Run();&lt;/div&gt;}&lt;/div&gt;}&lt;/div&gt;}&lt;br&gt;&lt;br&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt;&lt;font color=#0000FF&gt; static&lt;/font&gt;&lt;font color=#0000FF&gt; void&lt;/font&gt; SetForeground() {&lt;div style="margin-left:20pt"&gt;&lt;font color=#008000&gt;// Find all processes having the same name&lt;/font&gt;&lt;br&gt;Process[] processes = Process.GetProcessesByName(assemblyName);&lt;br&gt;&lt;font color=#0000FF&gt;foreach&lt;/font&gt; (Process process&lt;font color=#0000FF&gt; in&lt;/font&gt; processes)&lt;br&gt;{&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;if&lt;/font&gt; (process.Id == Process.GetCurrentProcess().Id)&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&lt;div style="margin-left:20pt"&gt;&lt;font color=#008000&gt;// This is the current process, pass&lt;/font&gt;&lt;br&gt;&lt;font color=#0000FF&gt;continue&lt;/font&gt;;&lt;br&gt;&lt;font color=#008000&gt;&lt;/div&gt;// Activates the other instance window by sending the message to any thread in the process.&lt;/font&gt;&lt;br&gt;&lt;font color=#0000FF&gt;foreach&lt;/font&gt; (ProcessThread thread&lt;font color=#0000FF&gt; in&lt;/font&gt; process.Threads)&lt;div style="margin-left:20pt"&gt;PostThreadMessage(thread.Id, WM_SHOWWINDOW, 0, 0);&lt;/div&gt;&lt;/div&gt;}&lt;/div&gt;}&lt;br&gt;&lt;br&gt;&lt;font color=#0000FF&gt;private&lt;/font&gt;&lt;font color=#0000FF&gt; static&lt;/font&gt;&lt;font color=#0000FF&gt; void&lt;/font&gt; Run() {&lt;div style="margin-left:20pt"&gt;&lt;font color=#0000FF&gt;using&lt;/font&gt;(MainForm form =&lt;font color=#0000FF&gt; new&lt;/font&gt; MainForm()) {&lt;div style="margin-left:20pt"&gt;IMessageFilter filter =&lt;font color=#0000FF&gt; new&lt;/font&gt; ThreadMessageFilter(form);&lt;br&gt;Application.AddMessageFilter(filter);&lt;br&gt;&lt;br&gt;&lt;font color=#0000FF&gt;try&lt;/font&gt;&lt;br&gt;{&lt;div style="margin-left:20pt"&gt;form.Show();&lt;br&gt;Application.Run(form);&lt;/div&gt;}&lt;br&gt;&lt;font color=#0000FF&gt;finally&lt;/font&gt; {&lt;div style="margin-left:20pt"&gt;Application.RemoveMessageFilter(filter);&lt;/div&gt;}&lt;/div&gt;}&lt;/div&gt;}&lt;/div&gt;}&lt;/div&gt;}&lt;br&gt;&lt;/div&gt;&lt;div style="background-color:#ffffff"&gt;&lt;b&gt; . . .&lt;/b&gt;&lt;/div&gt;&lt;/div&gt;
  &lt;!-- Main() code end --&gt;&lt;img src ="http://www.pixvillage.com/blogs/devblog/aggbug/3030.aspx" width = "1" height = "1" /&gt;</description></item></channel></rss>