<?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>C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx</link><description>In many situation when something fails (exception is thrown) in the try block you want to retry that again. This could be a network timeout exception, or some other resource unavailability where you expect the same piece of try block code to succeed if</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#476028</link><pubDate>Sat, 01 Oct 2005 18:20:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:476028</guid><dc:creator>Ayende Rahien</dc:creator><description>One thing that worries it about having retry is that it's easy to get into infinite loop.&lt;br&gt;But that is minor, as your code showed.&lt;br&gt;&lt;br&gt;What is interesting is /where/ does it restart? On the begin block or on the line that had the exception</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#476324</link><pubDate>Mon, 03 Oct 2005 07:47:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:476324</guid><dc:creator>abhinaba</dc:creator><description>It restarts at the begin block. So you need to ensure that re-execution of code in between the begin and the point where the exception got thrown does not have any side effect</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#476356</link><pubDate>Mon, 03 Oct 2005 12:52:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:476356</guid><dc:creator>Thong Nguyen</dc:creator><description>I've already created an Action.Retry function that supports retry count and timeouts.&lt;br&gt;It uses closures to allow you to naturally use any code you want in the retry.&lt;br&gt;&lt;br&gt;object myObj = ...&lt;br&gt;&lt;br&gt;Actions.Retry&amp;lt;object&amp;gt;(delegate&lt;br&gt;{&lt;br&gt;   myObj.NetworkAccess();&lt;br&gt;}, TimeSpan.FromSeconds(10));&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;You can also specify which exceptions to catch and wait periods between retries....&lt;br&gt;&lt;br&gt;A bit more powerful than the &amp;quot;retry&amp;quot; keyword though I can see the &amp;quot;retry&amp;quot; being of some use.</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#476782</link><pubDate>Tue, 04 Oct 2005 12:03:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:476782</guid><dc:creator>Ramsey</dc:creator><description>There were discussions on C# forums sometime back on including retry in the language. Don't know about the current status though</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#478265</link><pubDate>Fri, 07 Oct 2005 18:57:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:478265</guid><dc:creator>dhchait</dc:creator><description>I tend to use a pattern similar to:&lt;br&gt;&lt;br&gt;foo() {&lt;br&gt;&lt;br&gt;  bool keepTrying = true;&lt;br&gt;  while ( keepTrying ) {&lt;br&gt;   try {&lt;br&gt;    string filename = showDialogBox();&lt;br&gt;    Stream stream = openstream(filename);&lt;br&gt;    writestufftostream(stream);&lt;br&gt;    stream.close(); &lt;br&gt;    keepTrying = false;&lt;br&gt;   } catch (Exception ex) { &lt;br&gt;    // prompt to retry fail etc.&lt;br&gt;    keepTrying = messagebox(&amp;quot;failed: &amp;quot; + ex.Message + &amp;quot; - keep trying?&amp;quot;);&lt;br&gt;   }&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;&lt;br&gt;pseudo-code but you get the idea.  a retry keyword would fit nicely into the above scheme, i tend to do this a lot - especially in gui apps where you show a dialog then try to use the input, and may need to go back and retry it if the input is bad.... </description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#490354</link><pubDate>Tue, 08 Nov 2005 18:29:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:490354</guid><dc:creator>mschaef</dc:creator><description>&amp;quot;What is interesting is /where/ does it restart? On the begin block or on the line that had the exception &amp;quot;&lt;br&gt;&lt;br&gt;This is one of the more interesting parts of Common Lisp. It has exception handling that allows exceptions to be handled before the stack is unwound. It's also possible for code to establish named (and unnamed) restarts that allow exception handlers to restart the code in particular spots. If you were writing a function that threw a 'bad argument' exception, you might offer two restarts: one to retry the call with another argument and one to abort the call and assume a particular return value. It's key that all of this can be defined and overridden by the developer.&lt;br&gt;&lt;br&gt;More information:&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html"&gt;http://www.nhplace.com/kent/Papers/Condition-Handling-2001.html&lt;/a&gt;</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#490584</link><pubDate>Wed, 09 Nov 2005 02:51:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:490584</guid><dc:creator>David Totzke</dc:creator><description>This is crazy talk and just begging for trouble.&lt;br&gt;&lt;br&gt;while (!Wrapper(downloadMgr)) ;&lt;br&gt;&lt;br&gt;So somebody kicks out your network cable and the Wrapper function starts to return really fast because whatever it is calling will wise up to the fact that the network is _gone_ and there you are in a 100% CPU tight loop that you can't kill.&lt;br&gt;&lt;br&gt;Been there, done that, bought the T-Shirt&lt;br&gt;</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#490693</link><pubDate>Wed, 09 Nov 2005 08:32:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:490693</guid><dc:creator>abhinaba</dc:creator><description>David you are taking things too literally. No one is ever going to loop infinitely in a tight loop trying to do something on the network :)&lt;br&gt;&lt;br&gt;This is just a example meaning that you put all the retyring logic in the wrapper function and call it eternally. So wrapper should have the logic of counting how many retries it made and fail when it goes above some value.</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#504152</link><pubDate>Thu, 15 Dec 2005 19:33:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:504152</guid><dc:creator>RichB</dc:creator><description>In the past I've done retries using C# anonymous delegates - ie the codeblock I want to retry is first wrapped in a closure which allows me to execute it numerous times. Having it inside a delegate means it suddenly has a common interface and so the retry code can be written just once for many different types of codeblocks.&lt;br&gt;&lt;br&gt;To put another way, I've used the Command Pattern, but my Do() function has been simplified to just calling a delegate.</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#505018</link><pubDate>Sat, 17 Dec 2005 18:50:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:505018</guid><dc:creator>Hugh Gleaves</dc:creator><description>Hi&lt;br&gt;&lt;br&gt;I do like this whole retry idea, and have posted about it on GotDotNet (though not without some heated debate!)&lt;br&gt;&lt;br&gt;&lt;a rel="nofollow" target="_new" href="http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=352875&amp;amp;Page=1#353303"&gt;http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx?id=352875&amp;amp;Page=1#353303&lt;/a&gt;&lt;br&gt;&lt;br&gt;I have proposed to MS that they consider extending the syntax of 'try' to include an option expression:&lt;br&gt;&lt;br&gt;try (expression)&lt;br&gt;{&lt;br&gt;code;&lt;br&gt;}&lt;br&gt;catch(Exception e)&lt;br&gt;{&lt;br&gt;handling;&lt;br&gt;}&lt;br&gt;&lt;br&gt;This captures (I think) the key issues and implememnts them in the langauge:&lt;br&gt;&lt;br&gt;1. A loop&lt;br&gt;2. A termination condition&lt;br&gt;3. Execution of the handler after all retries have failed.&lt;br&gt;&lt;br&gt;I have also thought that this may be better, but this is just an idea:&lt;br&gt;&lt;br&gt;try (retries,interval)&lt;br&gt;{&lt;br&gt;code;&lt;br&gt;}&lt;br&gt;catch (Exception e)&lt;br&gt;{&lt;br&gt;handler;&lt;br&gt;}&lt;br&gt;&lt;br&gt;This is better, because it forces the coder to define a) the number or repetitions (before the handler finally runs) and b) a sleep interval to avoid tigh cpu loops (but they could put 0.&lt;br&gt;&lt;br&gt;The idea here is to provide a construct&lt;br&gt;&lt;br&gt;Both of these constructs can be expanded to a while loop etc, and would be automatically generated by the compiler, much as a C# iterator gets expanded.&lt;br&gt;</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#511470</link><pubDate>Wed, 11 Jan 2006 08:41:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:511470</guid><dc:creator>David Betz</dc:creator><description>&lt;a rel="nofollow" target="_new" href="http://davidbetz.net/winfx/2006/01/constrainted-try-with-retry-mechanism.aspx"&gt;http://davidbetz.net/winfx/2006/01/constrainted-try-with-retry-mechanism.aspx&lt;/a&gt;</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#643826</link><pubDate>Fri, 23 Jun 2006 10:38:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:643826</guid><dc:creator>lesbian rape</dc:creator><description>Best of all people w can talk...</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#644371</link><pubDate>Fri, 23 Jun 2006 18:40:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:644371</guid><dc:creator>Michael Hübner</dc:creator><description>In good old Smalltalk, the search for an appropriate exception handler leaves the stack in its current state (same as in Common List), i.e. does not unwind the call stack.&lt;br&gt;&lt;br&gt;This has the advantage, that in an exception handler (usually in the top handler) a debugger view can be opened, which allows you to inspect the life data which caused the exception, at the position in the code where the exception occured.&lt;br&gt;This safes you a lot of time when chasing errors.&lt;br&gt;</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#741172</link><pubDate>Tue, 05 Sep 2006 18:05:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:741172</guid><dc:creator>David Walker</dc:creator><description>&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/oldnewthing/archive/2005/11/07/489807.aspx"&gt;http://blogs.msdn.com/oldnewthing/archive/2005/11/07/489807.aspx&lt;/a&gt;&lt;br&gt;&lt;br&gt;From Raymond Chen:&lt;br&gt;&lt;br&gt;&amp;quot;I've seen this go wrong many times. So much so that my personal recommendation is simply never to retry automatically. If something fails, then report the failure. If the user wants to retry, let them be the ones to make that decision. &lt;br&gt;&lt;br&gt;Here's how it goes wrong. This is a real example, but the names have been removed because I'm not trying to ridicule anybody; I want you to learn. There was a networking feature that implemented some type of distributed networking capability. It is the nature of networks to be unreliable, so the implementors of the functionality decided to retry ten times before finally giving up. The operation they were performing was implemented by another group, and that other group also decided to retry five times before giving up. That second group called a networking function with a timeout of thirty seconds. Meanwhile, the application that used this networking capability attempted the operation fifteen times. &lt;br&gt;&lt;br&gt;Let's do some math. At the bottom was a timeout of thirty seconds. Five retries comes out to two and a half minutes. Ten retries from the next layer brings this to twenty-five minutes. Fifteen retries from the application layer takes us to over six hours. An operation that would normally have completed (with a failure code) in thirty seconds became, through the multiplicative effect of multiple layers of retrying, a six-hour marathon. And then you get a very angry call from one of your customers demanding that you deliver them a fix yesterday because this problem is taking down their entire sales force.&amp;quot;</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#4120128</link><pubDate>Sun, 29 Jul 2007 20:59:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:4120128</guid><dc:creator>Steven Oliver</dc:creator><description>&lt;p&gt;Thanks the ruby retry code. Exactly what I needed.&lt;/p&gt;
</description></item><item><title>Ruby.NET</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#6469889</link><pubDate>Thu, 22 Nov 2007 15:30:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6469889</guid><dc:creator>I know the answer (it's 42)</dc:creator><description>&lt;p&gt;I had blogged earlier about Gardens Point Ruby.NET . After I read Don Box writing about it, I decided&lt;/p&gt;
</description></item><item><title>re: C#: try and retry</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/01/c-try-and-retry.aspx#9559643</link><pubDate>Tue, 21 Apr 2009 19:26:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9559643</guid><dc:creator>mtsay</dc:creator><description>&lt;p&gt;another ugly method to work around the issue using existing construct and no goto:&lt;/p&gt;
&lt;p&gt;do&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp;try&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;...&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp;catch ( ... )&lt;/p&gt;
&lt;p&gt; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;...&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;continue;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;while(false);&lt;/p&gt;
</description></item></channel></rss>