System.InvalidOperationException: 'Object is currently in use elsewhere.'

on long running test (> 1000 documents) Aspose.Words.Document.Save(Stream stream, SaveFormat saveFormat) method throws subj error. This error doesn’t seem to be caused by a particular document as it processes them fine during shorter test. Aspose Words for Net v 14.10 Windows 8.1 Enterprise x64, running on single thread inside console app in 64 bit mode.

Hi Mtsaritsyn,

Thanks for your inquiry. Perhaps, you are getting this error because you are saving document in a loop. In this scenario, Document.Save method may be called for next iteration while first save operation is under process.

We suggest you please create separate thread for each document. The only thing you need to make sure is that you always use separate Document instances per each thread. Hope this helps you.

you’re correct, the documents are saved in the loop, as you can see each on its own thread even though in my case MAX_THREAD_COUNT was 1 so although each conversion happens on its own thread the total # of threads remains 1. The highlighted line throws the error::

foreach (FileData f in files)
{
int th_cnt;
lock (this._pdfList)
{
th_cnt = this._thCount;
}

while (th_cnt > MAX_THREAD_COUNT)
{
Thread.Sleep(20);

lock (this._pdfList)
{
th_cnt = this._thCount;
}
}

Thread th = new Thread(() => this.Convert(f, docConverterFactory, out_dir));
th.Start();

Thread.Sleep(20);
}

the actual conversion code returning content inside byte array:

using(MemoryStream ms = new MemoryStream())
{
this.SaveToPdf(ms);
ms.Position = 0L;
return ms.ToArray();
}

public override void SaveToPdf(Stream outputStream)
{
Document doc = this.GetDocument();

doc.Save(outputStream, SaveFormat.Pdf);
}

private Document GetDocument()
{
LoadOptions lo = new LoadOptions();
lo.LoadFormat = ConvertSupportedLoadFormat(this._contentType);

using (MemoryStream ms = new MemoryStream(this.InputContent))
{
Document result = new Document(ms, lo);
result.UpdateFields();

return result;
}
}

internally code uses its own Document instance created by docConverterFactory object, nothing is shared between these threads. The only thing they share is the output document folder but by that time document is already written internally into byte array, each into its own.

What would you suggest to fix the problem?

Hi Mtsaritsyn,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we'll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.

It seems latest version 16.3 doesn’t have this problem anymore. I’m still running the test over large set of documents and so far I haven’t seen single error over 20,000 + docs.

Hi Mtsaritsyn,

Thanks for your feedback. It is nice to hear from you that your problem has been solved using Aspose.Words for .NET 16.3.0. Please let us know if you face any issue while using Aspose.Words.