Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determining last update of a file when it is in construc
Message
 
À
25/03/2014 15:49:02
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01597298
Message ID:
01597316
Vues:
55
This message has been marked as the solution to the initial question of the thread.
>>There's a Window's event that fires when a new file is created. I'm not sure if it fires on first creation or when it's closed. Binding to that event is better than watching a folder for a new file.
>
>This is from a robot application that is executing a process on the file. So, there is an incoming and once completed, the robot needs to process it. The robot runs every minute. But, I need to make sure it doesn't grab a file if the transfer is still in progress. For now, I have put a verification to make sure the timestamp of the file is at least 20 seconds from Date.Now. I am not sure I can have a bind from the robot to an event in such infrastucture.


I am writing something very similar right now, I am ftping 75Mb files and when they are ready I need to parse them, so when I detect a new file with the FileSystemWatcher class I wait until the file is ready by trying to open it exclusively, this is my code:
        protected void WaitUntilFileIsReady(System.String fileName)
        {
            if (_eventLogger != null)
            {
                _eventLogger.LogEvent(string.Format("Waiting for file availability ({0} current size: {1} bytes", fileName, GetFileSize(fileName).ToString(System.Globalization.CultureInfo.InvariantCulture)), EventTypes.Wait);
                while (!IsFileReady(fileName))
                    System.Threading.Thread.Sleep(50);
                _eventLogger.LogEvent(string.Format("The file {0} is available! ({1}) bytes", fileName, GetFileSize(fileName).ToString(System.Globalization.CultureInfo.InvariantCulture)), EventTypes.Available);
            }
        }

        protected static bool IsFileReady(System.String fileName)
        {
            // If the file can be opened for exclusive access it means that the file
            // is no longer locked by another process.
            try
            {
                using (System.IO.FileStream inputStream = System.IO.File.Open(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
                {
                    return (inputStream.Length >= 0); 
                }
            }
            catch (System.Exception)
            {
                return false;
            }
        }
You can ignore the _eventLogger, as that is something I use to write into Windows Event Logger (and a SQL DB) to track activity.
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform