Solved

Determine that PDF has completed writing to disk

Posted on 2014-03-18
6
272 Views
Last Modified: 2014-03-31
Everyone:

Have small VB.net program (2010) that utilizes the "FileSystemWatcher" control.  Control "watches" for the creation of a particular PDF file, it then moves the file to a predetermined location.  PDF's are created by scanning to the "watched" file.  Scans with fewer than about 30 pages are handled correctly.  Larger scans are the problem.

Problem:
I was getting a corrupted PDF in the move to location on larger scans because the code in the "watched" section was writing the file before the very large scans could complete.  To solve that problem, I put a sleep statement to just pause the program to give the system time to fully write the PDF to file.  Solved my scan problem.  Put a sleep time of 60 seconds.

My solution has now created an additional problem of not allowing other processes to work during the 60 seconds.  Critical programs that need to run.

Now for my question:  Within the subroutine that is triggered by the creation of the file, how do I check that a file is "Completely written to disk, and there is no more activity?  How to do this without using a sleep statement?

So far I've attempted to check file size, but not much luck there.

If possible, just a very short snippet of code would be very helpful, or a very detailed explanation as to how to solve my problem.

Thanks in advance!!

Rick Norris
0
Comment
Question by:Rick Norris
  • 3
  • 3
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39938466
Without knowing the size of the file being sent, there is no way to absolutely know when a file has been completely written. All that remain are hacks, one of which is to try and obtain an exclusive lock on the file. If you get the lock, then the file is not in use; otherwise, you would need to yield control (probably by sleeping) to another thread for a period, and then recheck the lock later.
0
 

Author Comment

by:Rick Norris
ID: 39938586
Correct.... No way to know size... Largest file scaned so far is a little over 300 pages.  I can implement your suggestion; however... How can I "sleep" on a "separate" thread so that it will not intfere with another program??? Does that make sense?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39938712
So I was thinking it would be something along these lines:

Imports System.Threading
Imports System.IO

Public Class Form1
    Public Event Test As WaitCallback

    Private Sub FileSystemWatcher1_Created(sender As System.Object, e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
        Dim state As New MonitorData() With {.FilePath = e.FullPath, .Callback = AddressOf WhenFileCompleted}

        ThreadPool.QueueUserWorkItem(AddressOf FileCompletionMonitor, state)
    End Sub

    Private Sub WhenFileCompleted(ByVal filepath As String)
        MessageBox.Show(filepath + " completed!")
    End Sub

    Private Sub FileCompletionMonitor(ByVal state As Object)
        Dim parameters As MonitorData = DirectCast(state, MonitorData)

        While True
            Try
                Using handle As FileStream = File.Open(parameters.FilePath, FileMode.Open, FileAccess.Read, FileShare.None)
                    Exit While
                End Using  ' Using makes sure the file is closed if we acquire the lock. Don't want to lock the file for later processing
            Catch ex As Exception
                ' File lock not obtained
                Thread.Sleep(250)
            End Try
        End While

        parameters.Callback.Invoke(parameters.FilePath)
    End Sub
End Class

Public Class MonitorData
    Public Property FilePath As String
    Public Property Callback As Action(Of String)
End Class

Open in new window


In your FSW handler, you spawn off a new thread that will take care of the checking (and sleeping--only if you don't get the lock). Once you acquire the lock, the file *should* be done writing. At that point, you can invoke a callback method that you can pass the completed file's filename to. The callback's target can handle the processing that you wish to occur once the file i completed.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Rick Norris
ID: 39941117
kaufmed:

I'm sorry it's taken me so long to get back.  I am a CPA, and it's extremely busy now.  It looks like your comment above will definitely take care of my problem.  I've copied your code (THANK YOU VERY MUCH), and I will try it from home later this evening.

I will get back with you first thing tomorrow.

Thanks again.
Rick Norris
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39941234
No problem. Take your time.
0
 

Author Closing Comment

by:Rick Norris
ID: 39966331
Sorry it took so long to get back to you.  Your help was invaluable.  Your solution was "spot on"!

Rick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article focuses on how to remove password security from multiple PDF files by Adobe Acrobat program. Sometimes it is essential to access the stored data items and to print, edit as well as copy content from Portable Document Format files in abs…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now