Welcome Guest Search | Active Topics | Sign In | Register

Issue with threads and ConvertURL Options
malarkey
Posted: Wednesday, January 29, 2014 6:49:48 PM
Rank: Newbie
Groups: Member

Joined: 1/29/2014
Posts: 3
I am trying to implement a PDF generation utility which will take a URL and then convert the URL into a PDF. When I do this without threading it runs fine, but I would like to thread the process to speed it up. I am implementing a thread pool inside of a foreach loop that iterates through each URL that needs that needs to be converted, and the thread pool calls the EO pdf code to create the pdf file. When it gets to the ConvertURL call, all threads freeze up for a while until they finally throw an exception stating that I need to increase the MaxConcurrentTaskCount. This happens even if only 2 threads are running, and even if i set the MaxConcurrentTaskCount to a high number. Is there something I am missing here that needs to be done for this to work with threading?
eo_support
Posted: Wednesday, January 29, 2014 8:03:15 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

That's not normal. The default concurrency settings for EO.Pdf should allow more than 2 threads. You may want to check your thread code to see if you indeed has only 2 thread. If you believe you only have 2 threads but you are still getting the exception, you can try to isolate the problem into a small piece of test code and post the test code here. We will then try to run it here to see if we can see the same problem.

Thanks!
malarkey
Posted: Wednesday, January 29, 2014 8:54:20 PM
Rank: Newbie
Groups: Member

Joined: 1/29/2014
Posts: 3
I'm not sure if posting my code will be much help. This utility is used by an application to generate of PDF of the page the user is viewing, and this is done by passing the URL for this page along with other parameters to the utility. To have you be able to use it would mean I would have to rewrite some of the code for it to work outside of its current environment.

The strange thing is if I only have one thread run it works fine, but as soon as more than 1 thread tries to access the ConvertURL function it fails. If I debug this process all threads that are running fail at this same spot. I had initially tried this with a Parallel foreach loop, but was facing issues with concurrency and using a List, so I decided to use a ThreadPool. When using the Parellel foreach loop the conversion was working fine, it did not hang at the convertUrl call. Is there an issue with using a thread pool and calling the convertUrl method?

Below is my code - When this is run the threads will hang at the ConvertUrl call -

//Starts the thread pool for each url that needs to be converted.
foreach (IDictionary dictionary in parameters2)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state) { GeneratePDF(dictionary,parameters,directory); }));
}


//Generate PDF code
foreach (var dict in dictionary.Values)
{
Hashtable param = Utility.Cast<Hashtable>(dict);

urlForPdf = param["UrlForPdf"].ToString();
DealerName = param["DealerName"].ToString();

try
{
HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, PdfPageSizes.A4.Height, PdfPageSizes.A4.Width);
HtmlToPdf.Options.PageSize = new SizeF(PdfPageSizes.A4.Height + 1, PdfPageSizes.A4.Width + 1);
HtmlToPdf.Options.RepeatTableHeaderAndFooter = false;
HtmlToPdf.Options.AutoFitX = HtmlToPdfAutoFitMode.ScaleToFit;

PdfDocument PdfDoc = new PdfDocument();
HtmlToPdf.ConvertUrl(urlForPdf, PdfDoc);

documents.Add(PdfDoc);
}
catch (System.Exception exc)
{
System.IO.File.WriteAllText(String.Concat(directory, "\\Exception.txt"), exc.ToString());
//Add some better exception logic here. Return error page or something.
throw;
}
}
eo_support
Posted: Thursday, January 30, 2014 3:56:09 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

Whether you are calling the converter from a thread pool thread shouldn't matter. It should not cause ConvertUrl to hang. You may want to check your event viewer to see if you can see any additional information.

One scenario that can cause the converter to reach MaxConcurrentTaskCount is when you keep calling QueueUserWorkItem faster than ConvertUrl can completes. For example, if each ConvertUrl takes 5 seconds and you call QueueUserWorkItem every one second, then the number of uncompleted ConvertUrl calls will acumulate and eventually the conversion engine will hit MaxConcurrentTaskCount.

I did notice that you did not lock your dictionary Hashtable that you pass from the calling thread to the thread pool thread. That can cause problem if your Hashtable class is not already a synchronized Hashtable class.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.