Skip to content

Instantly share code, notes, and snippets.

@SpeedOfSpin
Created January 8, 2018 16:01
Show Gist options
  • Save SpeedOfSpin/dff77c3b47ea9cec88d1da69a8cc0efe to your computer and use it in GitHub Desktop.
Save SpeedOfSpin/dff77c3b47ea9cec88d1da69a8cc0efe to your computer and use it in GitHub Desktop.
Wait for a file to be ready
public static void WaitFileReady(string fileName)
{
while (true)
{
try
{
using (Stream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
if (stream != null)
{
Trace.WriteLine(string.Format("Output file {0} ready.", fileName));
break;
}
}
}
catch (FileNotFoundException ex)
{
Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
}
catch (IOException ex)
{
Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
}
catch (UnauthorizedAccessException ex)
{
Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
}
Thread.Sleep(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment