0

Trying to find the most elegant solution for a situation when I get a "IOException: Sharing violation" on loading a JSON file because it is being saved at the same time by another application.

Using a "try Catch' with some form of recursion when loading, though inelegant, makes sense. So after searching came across this C# solution which gets a lot of up votes Cleanest way to write retry logic?

The actual function call where my load occurs is

 private static T LoadData<T>(string filePath)
    {
        return JsonUtility.FromJson<T>(File.ReadAllText(filePath));
    }

However not sure how to implement using the above linked solution (Retry.Do) which doesn't seem to allow passed arguments in the function call ie

Retry.Do(SomeFunctionThatCanFail, TimeSpan.FromSeconds(1));

Can anyone help?

Bachalo
  • 6,965
  • 27
  • 95
  • 189

1 Answers1

0

You need to create a closure.

The simplest way is to use a lambda expression:

YourType data = Retry.Do(() => LoadData<YourType>("somefilepath"), TimeSpan.FromSeconds(1));
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35