// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Threading; namespace osu.Game.Utils { public static class FileUtils { /// /// Attempt an IO operation multiple times and only throw if none of the attempts succeed. /// /// The action to perform. /// The provided state. /// The number of attempts (250ms wait between each). /// Whether to throw an exception on failure. If false, will silently fail. public static bool AttemptOperation(Action action, T state, int attempts = 10, bool throwOnFailure = true) { while (true) { try { action(state); return true; } catch (Exception) { if (attempts-- == 0) { if (throwOnFailure) throw; return false; } } Thread.Sleep(250); } } /// /// Attempt an IO operation multiple times and only throw if none of the attempts succeed. /// /// The action to perform. /// The number of attempts (250ms wait between each). /// Whether to throw an exception on failure. If false, will silently fail. public static bool AttemptOperation(Action action, int attempts = 10, bool throwOnFailure = true) { while (true) { try { action(); return true; } catch (Exception) { if (attempts-- == 0) { if (throwOnFailure) throw; return false; } } Thread.Sleep(250); } } } }