Hi, I am trying to export the slides to HTML in parallel. However I get random exceptions in presentation.Save()
Could you please let me know how to accomplish this?
Presentation presentation = new Presentation();
ISlide firstSlide = presentation.Slides[0];
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
presentation.Slides.AddEmptySlide(firstSlide.LayoutSlide);
ConcurrentBag<string> slides = new ConcurrentBag<string>();
ResponsiveHtmlController controller = new ResponsiveHtmlController();
HtmlOptions htmlOptions = new HtmlOptions { HtmlFormatter = HtmlFormatter.CreateCustomFormatter(controller) };
Parallel.ForEach(presentation.Slides, (pptSlide) =>
{
try
{
using (MemoryStream stream = new MemoryStream())
{
presentation.Save(stream, new[] { pptSlide.SlideNumber }, SaveFormat.Html, htmlOptions);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
var html = reader.ReadToEnd();
slides.Add(html);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("SlideNumber: " + pptSlide.SlideNumber + " - " + ex.Message);
}
});
These are the sample of the random exceptions:
Debug Trace:
SlideNumber: 8 - An item with the same key has already been added.
SlideNumber: 4 - An item with the same key has already been added.
SlideNumber: 5 - An item with the same key has already been added.
SlideNumber: 6 - Collection was modified; enumeration operation may not execute.
SlideNumber: 1 - Collection was modified; enumeration operation may not execute.
SlideNumber: 3 - An item with the same key has already been added.