|
Rank: Newbie Groups: Member
Joined: 3/24/2014 Posts: 4
|
Hi,
I am totaly new to this product, sorry it the question is duplicate, but I didn't find similar question in the forum. I was wondering why is everything static when I want to convert html to pdf. Especially those methods:
HtmlToPdf.Options.AfterRenderPage += On_AfterRenderPage; and HtmlToPdf.ConvertHtml(data, outputPath);
How can I be sure, that when doing more conversions simultaneously, that everything works correctly? What if I want to create two different handlers for every conversion and run both at a same time?
Thank you very much for your help! L.K.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
You do not need to worry about that. Each thread has a separate copy of HtmlToPdf.Options. So it will work just fine even if you call it in multiple threads simultaneously.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 3/24/2014 Posts: 4
|
eo_support wrote:Hi,
You do not need to worry about that. Each thread has a separate copy of HtmlToPdf.Options. So it will work just fine even if you call it in multiple threads simultaneously.
Thanks! Thank you for your fast answere! Yeah, this exactly fits to my observations how it behaves inside of Task.Run... It is not optimal, but I think we can go with this...
|
|
Rank: Newbie Groups: Member
Joined: 3/24/2014 Posts: 4
|
One more question to be sure: I see that when I use Tasks and async features of .NET45 I definetly can run in heavy problems, because there it is not Thread separation guaranteed (this would be my preferred solution, because working with Threads is in these days legacy). So I probably do need to wrap your code in class that manages single Threads to be able to run more conversions in Parallel. Do you have some expirience/samples with server side converters, that have to work with lot of conversions at a same time? This would help me to avoid potential problems. Thanks in advance.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi, We are not sure what you meant by "there is no thread separation guaranteed". The thread separation are guaranteed because each thread uses their own copy of values. If you do not like the static flavor, you can use one of the flavor that takes an HtmlToPdfOptions argument such as this one: http://www.essentialobjects.com/doc/4/eo.pdf.htmltopdf.converturl_overload_8.aspxThe code would be something like this:
Code: C#
//Create your own copy of HtmlToPdfOptions
HtmlToPdfOptions options = new HtmlToPdfOptions();
.....set your options member....
//Use your own copy of HtmlToPdfOptions
HtmlToPdf.ConvertUrl(url, file_name, options);
In either case, you should not have problem regardless you use the old days Thread class or the new fancy Tasks and async feature. Tasks and async feature are built on top of thread pool and they do not change anything beneath. Thus using Tasks or await (these are just syntax candy) makes no difference from the converter point of view. So there is no need for you to wrap your code inside "single thread". In another word, if you run into problems when using .NET 4.5's Tasks and async, the problem would be somewhere else. One thing you do need to keep in mind is that converting HTML to PDF is a rather expensive task --- it is both network intensive (during the load stage) and CPU intensive (during the conversion stage). So it's not that you have more thread it will run faster. For example, if your system only have a single CPU, then it would definitely help if you have 2 threads ---- while one is waiting on the network the other can use the CPU. However if you have 10 threads, then they will just be over competing and constantly rob each other off. In that case you will start to get time out errors and you are basically overloading your server and that's not an issue can be resolved by multi-threading. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 3/24/2014 Posts: 4
|
Hi,
thanks a lot for the hint with the overload method, I did not see it first time. "ConvertHtml(string html, Stream stream, HtmlToPdfOptions options)" sounds great for me.
just to explain my concerns with the thread separation: I was aware of this scenario: You create two tasks with two different options and then you start them. They will both run on background pool as you write correctly, but it is not guaranteed that they will use different ThreadID, so it might happen, that they would mix up their options.
Thank you for the explanations!
L.K.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
I see. Yes you are right. If you use the static options, the only way to guarantee you get the correct options is to put the code that sets the option and HtmlToPdf.ConvertUrl calls inside the same task function. If one is inside the task function and another is outside of the task function, it's almost certain that they will get mixed up. In any case, the overloads with the options argument will probably make things easier since the value is passed as argument instead of relying on thread static variables.
Thanks!
|
|