int noOfAttempts = 3;
void StartServer();
bool IsServerRunning();
I need to re-attempt StartServer() 3 times based on result from IsServerRunnig(). Something like this:
StartServer();
if (!IsServerRunning())
{
StartServer();
if (!IsServerRunning())
{
StartServer();
if (!IsServerRunning())
{
StartServer();
}
}
}
I don’t want to use for loops or the ugly code like above. Is there a better way of doing this? A way in which, I won’t have to change my code if noOfAttempts change in future?
EDIT:
I’m expecting something from the “delegate” concept (If possible).
UPD:
Seems like you actually want(ed) some neat way to describe retry logic. In this case look at transient-fault-handling libraries such as Polly.
Ok.
Delegate magic here:
To esteemed downvoters:
It’s just for fun and I’d never write this code for real projects.