Add a Retry Mechanism for your code

Source: Internet
Author: User

Add a Retry Mechanism to the code


1. for you who often deal with network programming, the server will give you the desired response instead of every request. Although the Retry Mechanism does not solve this situation, it can greatly reduce this situation.

 

2. Introduce the Retry Mechanism class: retryutil. CS

With the use of delegation, the code is very short and not difficult to understand.

 1     public class RetryUtil 2     { 3         public delegate void NoArgumentHandler(); 4         /// <summary> 5         /// retry mechanism without argument 6         /// </summary> 7         /// <param name="retryTimes">try times</param> 8         /// <param name="interval">time span</param> 9         /// <param name="throwIfFail">throw exception</param>10         /// <param name="function">function name</param>11         public static void Retry(int retryTimes, TimeSpan interval, bool throwIfFail, NoArgumentHandler function)12         {13             if (function == null)14                 return;15 16             for (int i = 0; i < retryTimes; ++i)17             {18                 try19                 {20                     function();21                     break;22                 }23                 catch (Exception)24                 {25                     if (i == retryTimes - 1)26                     {27                         if (throwIfFail)28                             throw;29                         else30                             break;31                     }32                     else33                     {34                         if (interval != null)35                             Thread.Sleep(interval);36                     }37                 }38             }39         }40     }
View code

 

3. Example: Demon

3.1 download an object. If an error occurs five times and the interval is 2 seconds, an exception is thrown if all objects fail to be downloaded.

1                 RetryUtil.Retry(5, TimeSpan.FromSeconds(2), true, delegate2                 {3                     WebClientUtil.DownloadFile(string.Format("{0}{1}", baseUrl, tdNewSeries), 30000, dexsrp);4                 });

3.2 search for outlook emails. If an error occurs five times and the interval is 2 seconds, an exception is thrown if all emails fail.

 1         public List<EmailMessage> GetSearchQueryEmailMessage(string mailbox, string subjectKeyword, DateTime startDate, DateTime endDate, string sendAddress = "", string mailFolderPath = @"Inbox", string bodyKeyword = "") 2         { 3             List<EmailMessage> emails = null; 4             this.query = new EWSMailSearchQuery(sendAddress, mailbox, mailFolderPath, subjectKeyword, bodyKeyword, startDate, endDate); 5  6             RetryUtil.Retry(5, TimeSpan.FromSeconds(2), true, delegate 7             { 8                 emails = EWSMailSearchQuery.SearchMail(service, query); 9             });10 11             return emails;12         }

3.3 when you attempt to access a webpage, if an error occurs five times, the interval is 2 seconds. If all attempts fail, an exception is thrown.

1                 RetryUtil.Retry(5, TimeSpan.FromSeconds(2), true, delegate2                 {3                     htc = WebClientUtil.GetHtmlDocument(sourceUrl, 3000);4                 });

 

Add a Retry Mechanism for your code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.