Skip to content

Instantly share code, notes, and snippets.

@fredgdaley2
Last active April 7, 2022 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredgdaley2/e61aa4e05f4250bec7feb43385a838c3 to your computer and use it in GitHub Desktop.
Save fredgdaley2/e61aa4e05f4250bec7feb43385a838c3 to your computer and use it in GitHub Desktop.
Asynchronously wait for file to be accessible
private async Task<bool> WaitForFileToBeAccessible(string fileWaitingOn, int retryThreshold = 1000)
{
int numberOfRetries = 0;
while (numberOfRetries < retryThreshold)
{
try
{
using var fs = new FileStream(fileWaitingOn, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 100);
numberOfRetries++;
fs.ReadByte();
return true;
}
catch (Exception ex)
{
await Task.Delay(5000);
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment