Try n times

A method to call another method n times or until it returns true (success). Used to talk to wireless devices in a noisy environment where each call has a chance of failure.

bool Try(uint n, Func<bool> func)
{
int i = (int)(n == 0 ? 1 : n); do { if (func()) return true; i--; } while (i > 0); return false; }

Call it this way

return Try(3, ()=> { return Internal(p); });

Posted in Articles and tagged , .

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *