L. Porfido
L. Porfido

Reputation: 27

C# print a pdf and overlap of process

When I print files A.pdf and B.pdf, I run this code:

if (File.Exists(@"C:\Users\Luca\Desktop\A.pdf") == true && File.Exists(@"C:\Users\Luca\Desktop\B.pdf") == true)
        {
            using (PdfDocument one = PdfReader.Open(@"C:\Users\Luca\Desktop\A.pdf", PdfDocumentOpenMode.Import))
            using (PdfDocument two = PdfReader.Open(@"C:\Users\Luca\Desktop\B.pdf", PdfDocumentOpenMode.Import))
            using (PdfDocument outPdf = new PdfDocument())

            {
                CopyPages(one, outPdf);
                CopyPages(two, outPdf);

                outPdf.Save(@"C:\Users\Luca\Desktop\C.pdf");
            }
        }

but the program shows me an error:

"System.IO.IOException: 'The process cannot access the 'C: \ Users \ Luca \ Desktop \ B.pdf' file because it is in use by another process.'.

I try to use the method Task.Delay(500); to allow time to print, but the error shows again

Upvotes: 0

Views: 107

Answers (1)

capra314cabra
capra314cabra

Reputation: 106

It causes when opening file failed before and the file stream still open, not been closed. When I encountered a similar error, rebooting my PC to close all apps solved this error.

P.S.
The following code never stops your program.

Task.Delay(500);

We have to wait for a task to be completed.

Task.Delay(500).Wait();

Upvotes: 1

Related Questions