We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert PDF to JPG cause stuck when run multithread

Hi guys,
I’m using the following code snippet to convert all pages in PDF file to JPG image. It works fine when running single thread. But when I run it with multithread, up to 10 threads, the code stucked in imageDevice.Process() infinitely and there is no exception was thrown.
class Program
{
static void ConvertPdfToJpg(object input)
{
var outputFolder = @“D:\duplicate_out”;
var inputFile = (string)input;
ImageDevice imageDevice = new JpegDevice(new Resolution(100), 100);
using(Document pdfDocument = new Document(inputFile))
{
var filename = Path.GetFileNameWithoutExtension(inputFile);
for(int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
try
{
using(FileStream imageStream = new FileStream(Path.Combine(outputFolder, filename + pageCount + “.jpg”),
FileMode.Create))
{
imageDevice.Process(pdfDocument.Pages[pageCount], imageStream);
imageStream.Close();
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
continue;
}
}
}
}

    static void Main(string[] args)
    {
        var inputFolder = @"D:\duplicate";
        var files = Directory.GetFiles(inputFolder);
        var threads = new List<Thread>();
        foreach(var file in files)
        {
            var workerThread = new Thread(new ParameterizedThreadStart(ConvertPdfToJpg));
            workerThread.Start(Path.Combine(inputFolder, file));
            threads.Add(workerThread);
        }

        foreach(var thread in threads)
            thread.Join();

        Console.ReadKey();
    }
}

I’m using Aspose PDF for NET 19.11, Windows 10, Ram 32gb, .NET 4.0 and this file sample sample.pdf (6.7 MB). You can clone the sample file and test.
Thanks.

@dunghnguyen

We were unable to replicate the issue in our environment that you have mentioned. Would you kindly share a sample console application which is able to replicate the same issue which you are facing. We will again test the scenario in our environment and address it accordingly.

Hi @asad.ali,
This is my sample application Test.zip (4.4 KB). I also added some log to console and on my end, it stucked after converted about 50 pages/file.
Thanks.

@dunghnguyen

We have tested the scenario using your sample application and were still unable to notice any issue. For your kind reference, a screenshot is also attached. threadresults.png (16.2 KB) Could you please share some more details like screenshots or some special configurations that you are running and may help us replicating the issue at our side.

Hi @asad.ali
Please clone the sample file to 10 files and run the above test code with them. The problem only occurs when run multithread. The following screenshot which I run the above code with 10 files threadresult.PNG (67.1 KB). The expected result is the output folder should have 1960 images, but there is only 366 items.
Thanks.

@dunghnguyen

We were able to replicate the issue by creating 10 copies of shared document and putting them into source folder. Thus, we have created an issue as PDFNET-47343 in our issue tracking system for detailed investigation. We will further look into details of it and keep you posted with its rectification status. Please spare us little time.

We are sorry for the inconvenience.

Hi @asad.ali
Thanks for your support.

Hi @asad.ali,
Is there any update for this issue?
Thank you

@dunghnguyen

Sadly, the earlier logged issue is not yet resolved. We will surely work over fixing it as soon as investigation against it is completed and let you know once we have additional information in this regard. Please give us some time.

We are sorry for the inconvenience.