Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good and clean way to wait until the file closes [duplicate]

Tags:

Possible Duplicate:
Wait until file is unlocked in .NET

I have an open file, like a .Doc or .txt, and I have to wait until the user close it. I already try this, according to Wait until file is unlocked in .NET :

while (true) {     try     {       using (FileStream Fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 100))         {             //the file is close             break;         }     }     catch (IOException)     {         //wait and retry         Thread.Sleep(1000);     } } 

This works well ,but it may be possible to find a solution without a try/catch and handler the exception ?

like image 972
Guillaume V Avatar asked Sep 09 '11 19:09

Guillaume V


1 Answers

Unfortunately no, there is no other way.

The API doesn't have an event that will fire when a file in unlocked or anything else that is convenient.

Retrying with waits is the best solution with the current API.

like image 114
Oded Avatar answered Sep 28 '22 16:09

Oded