Quantcast
Channel: Aspose.Pdf Product Family
Viewing all articles
Browse latest Browse all 3131

HtmlFragment causing errors in multi-threading code

$
0
0
I am running into issues while generating PDFs containing HtmlFragments using multi-threading. I tried to narrow down the problem to html fragment in my code. It would be helpful if you could provide a workaround or a hotfix to solve this problem. Thank you. 

Please uncomment the commented line doc.Pages[1].Paragraphs.Add(htmlFragment); to reproduce the problem. The idea is to generate the files concurrently to save time. I am using the Aspose.Pdf v10.3 (not generator).

Attached the partial stack trace to hide the project code details. 

        [Test]
        public void ShouldTestAsposeThreadSafety()
        {
            License license = new License();
            license.SetLicense("Aspose.Pdf.lic");

            var list = new List<string>();
            for (int i = 0; i < 10; i++)
            {
                list.Add(i.ToString("0000") + ".pdf");
            }

            try
            {
                Parallel.ForEach(list, file =>
                {
                    Document doc = new Document();
                    doc.Pages.Add();
                    TextFragment textFragment = new TextFragment("TEXT Fragment");
                    HtmlFragment htmlFragment = new HtmlFragment("<p>TEST HTML Paragraph</p>");
                    doc.Pages[1].Paragraphs.Add(textFragment);
                    //doc.Pages[1].Paragraphs.Add(htmlFragment); // PROBLEM IS HERE
                    string tmpFolder = @"C:\Users\test\Desktop\Temp";
                    string tmpFile = Path.Combine(tmpFolder, file);
                    doc.Save(tmpFile);
                });
            }
            catch (AggregateException aex)
            {
                foreach (Exception ex in aex.Flatten().InnerExceptions)
                {
                    Console.WriteLine("Exception -->");
                    Console.WriteLine(ex);
                }
            }
        }

Regards,
Anil


Viewing all articles
Browse latest Browse all 3131

Trending Articles