Error "The process cannot access the file 'c:\Temp\tmpFile.tmp_img0.jpg' because it is being used by another process"

I have an application that saves a Cells workbook to a PDF document. This document contains a jpeg image.

If I save the document twice, I receive an exception while trying to save the second one:

System.IO.IOException: The process cannot access the file 'c:\Temp\tmpFile.tmp_img0.jpg' because it is being used by another process.

The line that is throwing the exception is:

mWorkbook.Save(stream, FileFormatType.AsposePdf)

When I stop the application and restart it, there is no problem saving once (but not twice).

I know for sure that this image is my header.

There is no Dispose method on the Workbook object. Setting my mWorkbook object or not does not fix the exception. Is there any resource I should free?

Hi,

Thanks for considering Aspose.

Well, Aspose.Cells for .NET is pure .NET component and it generally relies on .NET Garbage collection to release the resources against objects. Could you give us details why you save it to stream twice at the same time. When you call the save method of the workbook, after saving the book to a format, the workbook object (with its related object plus data) would be null. we appreciate if you could provide your code with template file here, we will check it soon.

Thank you.

>>Could you give us details why you save it to stream twice at the same time.

The user is allowed to select some criteria for the report and click a button to generate it. It seems that a handle is still holding the image.

Hi,

Could you try to save the .xml file (Aspose.Pdf integrated) to disk instead of stream.

E.g.,

workbook.Save("d:\\test\\bookpdf.xml",FileFormatType.AsposePdf);

Thank you.

here is a sample of code. Just click the button twice and you will get the error the second time the code runs.

Private mWorkbook As Aspose.Cells.Workbook
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'create a new empy Excel document

mWorkbook = New Workbook()

mWorkbook.Worksheets.RemoveAt(0)


Dim objSheet As Worksheet = mWorkbook.Worksheets.Add("Test")


objSheet.Pictures.Add(0, 0, Application.StartupPath + "\namelogo.jpg")


'save as PDF


'Save the document in Aspose.Pdf.Xml format

Dim stream As IO.MemoryStream = New IO.MemoryStream


mWorkbook.Save(stream, FileFormatType.AsposePdf)

stream.Seek(0, IO.SeekOrigin.Begin) '// Reset the pointer to the beginning of the stream



'// Load the document as raw XML

Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument

xmlDoc.Load(stream)


stream.Close()

stream.Dispose()

stream = Nothing


'Read the file in Aspose.Pdf.Xml format into Aspose.Pdf

Dim pdf1 As Aspose.Pdf.Pdf = New Aspose.Pdf.Pdf()


'Bind the XML file (containing spreadsheet data) with the Pdf object

pdf1.BindXML(xmlDoc, Nothing)


'Create the PDF document by calling its Save method

pdf1.Save(Application.StartupPath + "\test.pdf")

'pdf1.Close()

pdf1 = Nothing


mWorkbook = Nothing

End Sub

exactly the same exception is raised (only filename changes):

System.IO.IOException: The process cannot access the file 'C:\_Dev\Matis\TestReporting\bin\Debug\MySpreadsheet.xml_img0.jpg' because it is being used by another process.

Hi,

I could not reproduce the error. Which version of Aspose.Cells you are using? Did you try the latest 4.4.1.

And by the way could you separate Aspose.Pdf code and try the following code twice at the same time if it works fine.

Private mWorkbook As Aspose.Cells.Workbook


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'create a new empy Excel document

mWorkbook = New Workbook()

mWorkbook.Worksheets.RemoveAt(0)


Dim objSheet As Worksheet = mWorkbook.Worksheets.Add("Test")


objSheet.Pictures.Add(0, 0, Application.StartupPath + "\namelogo.jpg")


'save as PDF


'Save the document in Aspose.Pdf.Xml format

Dim stream As IO.MemoryStream = New IO.MemoryStream


mWorkbook.Save(stream, FileFormatType.AsposePdf)

stream.Seek(0, IO.SeekOrigin.Begin) '// Reset the pointer to the beginning of the stream



'// Load the document as raw XML

Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument

xmlDoc.Load(stream)


stream.Close()

stream.Dispose()

stream = Nothing

Thank you.

I have Aspose.Cells 4.4.1.3 (provided to me to fix another bug). I have also tried the plain 4.4.1 and got the same exception.

If I don't generate the PDF, there is no exception but I need the PDF.

Hi,

Please try this fix. And please download the latest Aspose.Pdf fix.I think it's caused by Multi threads.

In this fix,we use Cell.GetStyle and SetStyle method replace the property of Cell.Style. The two methods will save more memory usage than cell.Style property.

If you want to set style of the cell, please change your codes as :

//Getting the style of the cell.

Style style = cell.GetStyle();

//Setting the property of the style.

.......

//Setting the style of the cell

cell.SetStyle(style);