Solved

Determine that PDF has completed writing to disk

Posted on 2014-03-18
6
271 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 74

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 74

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 74

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

*Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

13 Experts available now in Live!

Get 1:1 Help Now