Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rerun the try block if we encountered an error? [duplicate]

Tags:

c#

Possible Duplicate:
C# cleanest way to write retry logic?

i having a function contains web service call to the server which fails sometime (unable to connect remote server error) due to some disturbance in network. The code is in try catch block. i want to rerun the web service call within try block so that the web call will be done successfully.

like image 635
Kumaran T Avatar asked Nov 27 '22 21:11

Kumaran T


1 Answers

const int MaxRetries = 5;

for(int i = 0; i < MaxRetries; i++)
{
   try
   {
       // do stuff

       break; // jump out of for loop if everything succeeded
   }
   catch(Exception)
   {
       Thread.Sleep(100); // optional delay here
   }
}
like image 57
Albin Sunnanbo Avatar answered Nov 29 '22 13:11

Albin Sunnanbo