<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/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>What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx</link><description>This time, I'll present a runnable program. The ConsoleCtrl class is designed to intercept CTRL-C and, for testing purposes, will exit after it gets 10 of them. Or after a little over 27 hours. 
 What's wrong with this code? 
 using System; using System</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>What's wrong with this code #7 - Discussion</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#422719</link><pubDate>Sat, 28 May 2005 00:12:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:422719</guid><dc:creator>Eric Gunnerson's C# Compendium</dc:creator><description>In this edition, I presented a small chunk of code that attempted to hook the signal handlers, so that...&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=422719" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#421123</link><pubDate>Mon, 23 May 2005 21:08:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:421123</guid><dc:creator>Phil Hochstetler</dc:creator><description>The most obvious thing I see wrong is assuming the count goes from 9 to 10.  The count could be incremented twice before the test so it would appear to go from 9 to 11.  Either a lock() should be added around the increment / test, or a  interlocked call used or just test for &amp;gt;=.  All would fix the problem.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=421123" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#421118</link><pubDate>Mon, 23 May 2005 20:50:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:421118</guid><dc:creator>mgd</dc:creator><description>I tend to agree with the idea that delegate may get GC'd; since it's passed into unmanaged code the GC will have no way of telling that it's still required. &lt;br&gt;&lt;br&gt;But since you can't count of when GC will or won't happen... I would be surprised if it was not usually be the case that it was still around.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=421118" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#421034</link><pubDate>Mon, 23 May 2005 16:55:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:421034</guid><dc:creator>Felipe Garcia</dc:creator><description>I think that &amp;quot;the code&amp;quot; will work fine. The problem I see is related to the Thread.Sleep that will hold the execution of the program for &amp;quot;a while&amp;quot; (100000 / 60) / 60 is like ~27.8 hours.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=421034" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#421003</link><pubDate>Mon, 23 May 2005 12:17:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:421003</guid><dc:creator>dhananjay singh</dc:creator><description>I think SetConsoleCtrlHandler internally creates new thread, so application will keep running even after count&amp;gt;10&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=421003" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#421002</link><pubDate>Mon, 23 May 2005 12:07:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:421002</guid><dc:creator>dhananjay singh</dc:creator><description>count++;&lt;br&gt;if (count == 10)&lt;br&gt;{&lt;br&gt; Environment.Exit(0);&lt;br&gt;}&lt;br&gt;&lt;br&gt;is not thread safe, so it is possible that even though count &amp;gt; 10 and progam is still running. ;)&lt;br&gt;&lt;br&gt;Use Interlocked.Increment(ref int)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=421002" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#420644</link><pubDate>Sat, 21 May 2005 03:02:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:420644</guid><dc:creator>Matt Garven</dc:creator><description>Not only may the ControlEventHandler get GC'd, but the ConsoleCtrl instance itself will also be eligible for garbage collection (an optimisation in release mode only - during debug it won't be eligible until the end of the method to allow debuggers to inspect the value). I think :)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=420644" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#420533</link><pubDate>Fri, 20 May 2005 21:25:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:420533</guid><dc:creator>Sheeshers</dc:creator><description>Where are you checking to make sure you only respond to a CTRL-C event? Or maybe you're looking for something more than just such trivial stuff?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=420533" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#420504</link><pubDate>Fri, 20 May 2005 20:01:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:420504</guid><dc:creator>Versus</dc:creator><description>Thread.Sleep(100000);&lt;br&gt;&lt;br&gt;note:&lt;br&gt;thats 100000 miliseconds = 100 seconds, not 27 hours&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=420504" width="1" height="1"&gt;</description></item><item><title>re: What's wrong with this code #7?</title><link>http://blogs.msdn.com/b/ericgu/archive/2005/05/20/420460.aspx#420499</link><pubDate>Fri, 20 May 2005 19:45:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:420499</guid><dc:creator>Andrew</dc:creator><description>The ControlEventHandler delegate should return bool.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=420499" width="1" height="1"&gt;</description></item></channel></rss>