The process cannot access the file ... because it is being used by another process

I'm developing a Web application, I use Aspose.Pdf to compile multiple pdf files into another one.

My code like this:

static string ConcatFiles(string sourceFile, string additionalFile)

{

var temp = Path.GetTempFileName();

File.Copy(additionalFile, temp, true); // Clone to temp file


var outFile = Path.GetTempFileName();

var pdfEditor = new PdfFileEditor();

pdfEditor.Concatenate(new[] { sourceFile, temp }, outFile);


File.Delete(temp); // delete temp file

return outFile;

}

void Main()

{

Parallel.For(0, 10000, i =>

{

ConcatFiles(@"D:\a.pdf", "D:\b.pdf");

});

}

I getting a error when loop ConcatFiles many times: at File.Delete(temp);

The process cannot access the file '....' because it is being used by another process

What Can I do ? Sorry for my terrible format.

Thank in advance.

I think PdfFileEditor.Concatenate does not release the file.

Hi Dung,

Thanks for contacting support.

I have tested the scenario using latest release of Aspose.Pdf for .NET 11.7.0 in Visual Studio 2010 project running over Windows 7 and I am unable to notice any issue. Can you please try using the latest release and see if it resolves your problem. In case the issue still persists, please share some details regarding your working environment i.e. Operating System, .NET Framework version etc.

We are sorry for this inconvenience.

[C#]

System.Threading.Tasks.Parallel.For(0, 10, i =>
{
    ConcatFiles(@"c:/pdftest/test.pdf", "c:/pdftest/EU28695-3.pdf");
});

static string ConcatFiles(string sourceFile, string additionalFile)
{
    var temp = Path.GetTempFileName();
    File.Copy(additionalFile, temp, true); // Clone to temp file
    var outFile = Path.GetTempFileName();
    var pdfEditor = new PdfFileEditor();
    pdfEditor.Concatenate(new[] { sourceFile, temp }, outFile);
    File.Delete(temp); // delete temp file
    return outFile;
}

Hi Nayyer,


Thanks for your respond,

This issue is very intermittent. As my sample code, I have to loop 10.000 times with 2 big files (about 40-50mb), and it will occur at some points.
I have licence on version 9.4, but I already tried with 11.7, the issues still occur.
I tested on Win 7 x64, .NET v4.0, Visual Studio 2013.

Can you please reproduce it again.
Thanks for your patient.

Hi Dung,


Thanks for sharing the details.

I am working on replicating the issue using recently shared information and will get back to you soon.
1 Like