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(int n, Func func)
{
  do {
    if (func())
      return true;
    n--;
  }
  while (n>0);
  return false;
}

Call it this way

return Try(3, ()=> { return Internal(p); });
Posted in Articles and tagged , .

2 Comments

Leave a Reply