We are converting multiple PDF documents to PNG images using multiple threads.
All our methods are thread-safe: no PDF document is processed by more than one thread at the same time, no Apsose objects are shared by multiple threads:
// Load the input PDF document
using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filePath))
{
// Render the page into a raster image
using (MemoryStream imageStream = new MemoryStream())
{
// Create Resolution object
Resolution res = new Resolution(resolution);
if (format == ImageSaveFormat.PNG)
{
// Create PNG device with specified attributes
PngDevice pngDevice = new PngDevice(res);
// Convert a particular page and save the image to stream
pngDevice.Process(pdfDocument.Pages[pageNo], imageStream);
}
else
{
JpegDevice jpegDevice = new JpegDevice(res);
jpegDevice.Process(pdfDocument.Pages[pageNo], imageStream);
}
imageBytes = imageStream.ToArray();
}
//Logger.Instance.WriteLine(LoggingLevel.Information, "Exctracted page #" + pageNo);
}
Everything works fine except of some rare cases when *random* deadlocks in pngDevice.Process occur. See screenshot for more info - they always happen in the same method. On screenshot you see 10 "Running" processes - all of them are in "deadlock" state.