Created
January 8, 2018 16:01
-
-
Save SpeedOfSpin/dff77c3b47ea9cec88d1da69a8cc0efe to your computer and use it in GitHub Desktop.
Wait for a file to be ready
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
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