PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Sporadic "The process cannot access the file " on save
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4347
Page 1 of 1

Author:  BigDanSRQ [ Tue Jun 14, 2022 12:31 am ]
Post subject:  Sporadic "The process cannot access the file " on save

We have some code that has been running for years that creates PDFs from a SSRS Report. We've created literally millions of documents thru this code.

Recently on our development server, we started getting sporadic "System.IO.IOException: The process cannot access the file 'xx786801.pdf' because it is being used by another process" messages on PdfSharp.Pdf.PdfDocument.Save Yes! We are using a unique file name, and this process has been working for years. No code changes; no hardware changes; and this is sporadic. Tried a reboot. The process will create 50-75 documents and then get this exception. we can continue with another 50-70 and another exception.

The code creating the document looks like this:
Code:
Dim stream As System.IO.FileStream = System.IO.File.Create(OutDir + FileName, result.Length)
        stream.Write(result, 0, result.Length)
        stream.Close()
        stream.Dispose()



And then we run a function that locks the PDF that looks like this:
Code:
 
        Dim document As PdfSharp.Pdf.PdfDocument = PdfReader.Open(Filename, MyPW)
        Dim SecuritySettings As PdfSharp.Pdf.Security.PdfSecuritySettings = document.SecuritySettings

        If PasswordRequiredOnOpen = True Then
            SecuritySettings.UserPassword = MyPW
        End If

        ' setting the owner password will restrict things like editing and printing
        SecuritySettings.OwnerPassword = MyPW
        SecuritySettings.PermitAccessibilityExtractContent = False
        SecuritySettings.PermitAnnotations = False
        SecuritySettings.PermitAssembleDocument = False
        SecuritySettings.PermitExtractContent = False
        SecuritySettings.PermitFormsFill = False
        SecuritySettings.PermitFullQualityPrint = True
        SecuritySettings.PermitModifyDocument = False
        SecuritySettings.PermitPrint = True

        document.Save(Filename)
        document.Close()
        document.Dispose()





Just not too sure where to even start on this. Any ideas?

Dan

Author:  TH-Soft [ Tue Jun 14, 2022 8:23 am ]
Post subject:  Re: Sporadic "The process cannot access the file " on save

Hi!

Why not use a MemoryStream to save the "file", then read the PDF from the MemoryStream, lock it, then create a file on disk.

Author:  BigDanSRQ [ Tue Jun 14, 2022 9:45 am ]
Post subject:  Re: Sporadic "The process cannot access the file " on save

I’m not soliciting for new ways to write the code. I’m trying to figure out why, after a million documents, I’ve started throwing sporadic exceptions.

Author:  TH-Soft [ Tue Jun 14, 2022 11:02 am ]
Post subject:  Re: Sporadic "The process cannot access the file " on save

BigDanSRQ wrote:
I’m trying to figure out why, after a million documents, I’ve started throwing sporadic exceptions.
I can only speculate about that.
Check what has changed at the time when exceptions started to appear.

New anti-virus software?
New .NET version?
Windows updates?

You write a file, close it, open it again, write it again.
AV software will check every newly created file. This could be the problem. And it is slow.
Maybe garbage collection or background tasks/clean-up tasks have changed in .NET or Windows.

You can invoke Save with a stream. Before calling Save, you can try to open the file in a loop so you can re-try when the exception occurs.

The redundant Open is suddenly causing problems. Best to phase out the redundant Open and speed up the process IMHO.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/