Answer the question
In order to leave comments, you need to log in
Is there a simpler “repeat until success” snippet?
while (true)
{
try
{
File.WriteAllText(@"C:\\serv1.txt", "text");
break;
}
catch (Exception)
{
Thread.Sleep(300);
}
}
Answer the question
In order to leave comments, you need to log in
Try this method (no way to test). The input is a delegate that needs to be executed.
void UntilSuccess(Action action) {
if (action == null)
throw new ArgumentNullException();
while (true) {
try
{
action.Invoke();
break;
}
catch { Thread.Sleep(300); }
}
}
// Использовать так:
UntilSuccess(() => File.WriteAllText(@"C:\\serv1.txt", "text"));
and if in a recursion?
type
private void writeText(){
try
{
File.WriteAllText(@"C:\\serv1.txt", "text");
}
catch (Exception)
{
Thread.Sleep(300);
writeText();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question