Problem with savinf doc as PDF with Encryption details and multithreading application

I have experienced problems when generating pdf in multi-threaded application with encryption detail options.
the application has been running successfully as a single threaded application. As soon as the multi threading was implemented and encrption details options added, i have the following error : L’objet est actuellement utilisé ailleurs.
Here are the stack trace :

System.Drawing.Graphics.GetHdc()
à .( , Graphics )
à .(String , Single , FontStyle )
à ​ .( , Single )
à ​ . ()
à . (Boolean )
à . (Boolean )
à . ()
à .( )
à .( , Boolean )
à .( )
à .( )
à .( )
à .()
à .()
à .()
à .( , Int32 )
à .( )
à .( )
à .( , Int32 )
à .( , Int32 , Boolean )
à .()
à .( , Int32 , Boolean , Boolean )
à .( , Boolean )
à .( )
à .( , Int32 , Boolean )
à .()
à .( , Int32 , Boolean , Boolean )
à .( )
à .( )
à .(Boolean )
à .( , Boolean )
à .( )
à .( )
à .(Boolean )
à .(Boolean )
à . ()
à .(Document , )
à Aspose.Words.Document.UpdatePageLayout()
à Aspose.Words.Document.(Boolean )
à Aspose.Words.Document. ()
à Aspose.Words.Document.get_PageCount()
à .( , )
à . ( )
à Aspose.Words.Document.(Stream , String , SaveOptions )
à Aspose.Words.Document.Save(Stream stream, SaveOptions saveOptions)
à AsConfManager.Utility.Aspose.AsposeWordHelper.ConvertAndencryptToPdf(Byte[] content, String userpassword, String ownerpassword) dans C:\Dev\Aspa\AsConfManager.Utility.Aspose.Word\AsposeWordHelper.cs:ligne 2211.

Here are the code :

Document doc = null;
using (MemoryStream str = new MemoryStream(content))
{
    doc = new Document(str);
}
using (MemoryStream outstream = new MemoryStream())
{
    PdfSaveOptions saveOptions = new PdfSaveOptions();
    // Create encryption details and set owner password.
    PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails(userpassword, ownerpassword, PdfEncryptionAlgorithm.RC4_128);
    // Start by disallowing all permissions.
    encryptionDetails.Permissions = PdfPermissions.DisallowAll;
    // Extend permissions to allow printing and extraction.
    encryptionDetails.Permissions =
    PdfPermissions.Printing | PdfPermissions.ContentCopy;
    saveOptions.EncryptionDetails = encryptionDetails;
    doc.Save(outstream, saveOptions);
}

Thank you.

Hi Tarik,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Thank you for your response.

I attach the console application with samples that cause the error.

Can you please try to fix this because we have to choose between your solution Aspose and an other solution, Fixing this issue that is serious, will be great for choosing Aspose as word management solution.

Thank you

Hi Tarik,

Thanks for sharing the detail. Unfortunately, I have not found the code in the shared zip file. Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document and share it here for testing purposes.

Thank you and sorry for the code.

Here are the project.

Thank you.

Hi Tarik,

Thanks for sharing the detail. Aspose.Words does support multi-threading. The only thing you need to make sure is that you
always use separate Document instances per each thread. One thread should use one Document object.

In your case, I suggest you please use the lock keyword in your code as shown below to fix your issue. Please read following links for your kind reference.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock
https://docs.microsoft.com/en-us/previous-versions/ms173179(v=vs.140)

static System.Object lockThis = new System.Object();

static void Main(string[] args)
{
    var fileinfo = Directory.GetFiles(@"..\..\docs");
    foreach (string s in fileinfo)
    {
        lock (lockThis)
        {
            Thread t = new Thread(convertpdf);
            t.Start(s);
        }
    }
}

public static void convertpdf(object s)
{
    try
    {
        lock (lockThis)
        {
            Document doc = new Document(s.ToString());
            using (Stream str = new MemoryStream())
            {
                Random r = new Random();
                PdfSaveOptions sv = new PdfSaveOptions();
                doc.Save(str, sv);
            }
        }
    }
    catch (Exception e)
    {
    }
}

Thank you.

I can’t do a lock iin this way because we want that all threads do the save at same time, in order to increase performance.

Can i do a lock only on line doc = new Document() ? do you have an example of such code ?

Thank you very much for help.

Hi Tarik,

Thanks for your inquiry. I have tested the
scenario and have managed to reproduce the same issue at my side. For
the sake of correction, I have logged this problem in our issue tracking
system as WORDSNET-11111. I have linked this forum thread to the same
issue and you will be notified via this forum thread once this issue is
resolved.

We apologize for your inconvenience.

Hello,

Any news about fix please ?

Thank you

Hi Tarik,

Thanks for your inquiry. I would
like to share with you that
issues are addressed and resolved based on first come first serve
basis. Currently, your issue is pending for analysis and is in the
queue. I am afraid, I can’t provide you any reliable estimate at the
moment. Once your issue is analyzed, we will then be able to provide you
an estimate.

Thank you for your patience and understanding.

The issues you have found earlier (filed as WORDSNET-11111) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

awesome! Thank you.