Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Problem running Convert() multiple times #4

Closed
raydk opened this issue Aug 23, 2012 · 11 comments
Closed

Problem running Convert() multiple times #4

raydk opened this issue Aug 23, 2012 · 11 comments

Comments

@raydk
Copy link

raydk commented Aug 23, 2012

Hi,

First of all, I would like to say that it's a very nice library you have created. Everything works fine, except for one thing.
I have created a simple application where I try to write a PDF upon a button click. The first time the PDF is created fast and with no problem, but the application just hangs forever without any errors after postback when i try to click the button again. Do you have any idea what the problem could be?

This is my sample code:

protected void Button1_Click(object sender, EventArgs e)
{
string targetFolderPath = String.Format("~/Resources/ProjectFiles/Project_{0}/Letters/{1}/", SessionWrapper.Current.currentProjectID, LetterType);
Response.Clear();
string pdfPath = String.Format(targetFolderPath + "/{0:yyyy-MM-dd_hh-mm-ss}helloworld.pdf",DateTime.Now);
byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert("

Hello world!

");
ByteArrayToFile(@Server.MapPath(pdfPath), pdfBuf);
}

@raydk
Copy link
Author

raydk commented Aug 23, 2012

Oops I didn't realise that the comment box supported HTML tags :D

@raydk
Copy link
Author

raydk commented Aug 23, 2012

I forgot to mention that the application hangs on this line on the second run.

byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert("Hello world!");

@gmanny
Copy link
Owner

gmanny commented Aug 23, 2012

Hello, thank you!

It's strange, because I designed, coded and tested Pechkin to be able to run multiple times. Could you please provide details about OS on which you're running the test?

And also, in this repo there's project PechkinTestApp, could you please check, if it works for you? (You'll need to check out all the code and open the solution, for the project to be compiled correctly.)

@raydk
Copy link
Author

raydk commented Aug 23, 2012

Thanks for your fast reply.

I'm running Windows 7 Pro using Visual Web Developer 2010.

I also think it's strange that it just hangs without any error. At first I thought it was because the FileStream wasn't disposed or something like that,but later on I realised that it didn't reach to the writing part before it started to hang.

I will try to download the PechkinTestApp project and see if it causes same issues.

@gmanny
Copy link
Owner

gmanny commented Aug 23, 2012

This test app really does just what you do, and it runs fine for me on Windows 7 Pro x64. Even with modification to use SimplePechkin.

So I need more details to reproduce the bug.

Also, you could try SynchronizedPechkin insted of Simple one, but within your one UI thread SimplePechkin is supposed to be fine.

@gmanny
Copy link
Owner

gmanny commented Aug 23, 2012

In fact, if it's web app (and not WinForms, as I thought before), you should use SynchronizedPechkin, since for every request there's new thread in web apps.

@raydk
Copy link
Author

raydk commented Aug 23, 2012

Well the test app runs fine, also multiple times. Perhaps there are some difference because my application is a webapp.

@gmanny
Copy link
Owner

gmanny commented Aug 23, 2012

Yeah, see comment above :)

@raydk
Copy link
Author

raydk commented Aug 23, 2012

Allright, I will try to use the SynchronizedPechkin, So what difference does it make if there's a new thread for every request? :)

@gmanny
Copy link
Owner

gmanny commented Aug 23, 2012

The undelying library (WkHtmlToPDF) isn't thread friendly, everything inside it gets messed up, when you work with it from multiple threads.

It really happens to all the code written without threading in mind :)

So SynchronizedPechkin creates one thread to work with the library and tunnels all the calls to this thread. This way it keeps the lib safe.

@raydk
Copy link
Author

raydk commented Aug 23, 2012

I see, I didn't give that any thought before I jumped into the simple app.
But you have saved me from a big headache because my application works now with the SynchronizedPechkin library! :D

Thank you very much for your help and keep up the good work! This library have saved me from a lot of trouble if I had to use iTextSharp or something similar.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants