I have a load of methods that throw exceptions sometimes, if they throw them I want to catch them, call a functino and then try them again a few times, here is what I am currently doing:
bool loop = true;
int i = 0;
while (loop)
{
try
{
loop = false;
if (gcDevice != null)
{
gcDevice.getStatus();
}
}
catch (Exception e)
{
if (i < lisenceErrorTries)
{
if (e.Message == "15: license key is not valid")
{
System.Console.WriteLine(e.Message);
Reconnect();
loop = true;
}
else
{
System.Console.WriteLine(e.Message + " after " + i + " tries");
}
}
else
{
System.Console.WriteLine(e.Message + " after " + i + " tries");
}
Thread.Sleep(20);
i++;
}
}
but the thing is, I need to do this loads and its getting really confusing writing it out all the time when all that changes is the line
gcDevice.getStatus();
what I need is something like a method where I can give it a method to perform inside the above, something like:
TryToDoThisWithRepeats(gcDevice.getStatus(), lisenceErrorTries);
Is there anything like this?
Thanks