Last active
April 7, 2022 00:19
-
-
Save fredgdaley2/e61aa4e05f4250bec7feb43385a838c3 to your computer and use it in GitHub Desktop.
Asynchronously wait for file to be accessible
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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