Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: PdfDocumentRenderer.RenderDocument() should support cancel/stop #19

Closed
heku opened this issue Jul 5, 2023 · 7 comments
Closed
Labels
support wontfix This will not be worked on

Comments

@heku
Copy link

heku commented Jul 5, 2023

Hi,

Because when creating a large PDF, the PdfDocumentRenderer.RenderDocument() may take several minutes to execute, it's better to add a API to allow cancelling/stopping it.

@TH-Soft
Copy link

TH-Soft commented Jul 5, 2023

It is possible to render documents page by page. If you do it this way, you can stop or cancel as well.

@TH-Soft TH-Soft closed this as completed Jul 5, 2023
@TH-Soft TH-Soft added wontfix This will not be worked on support labels Jul 5, 2023
@heku
Copy link
Author

heku commented Jul 5, 2023

Thanks, I will have a try.

@heku
Copy link
Author

heku commented Jul 7, 2023

Hi @TH-Soft , I tried your suggestion, yes I can render documents page by page, but before that, the call to PrepareRenderPages() still take time to execute, is it possible to provide a method or parameter to allow user cancel it?

@TH-Soft
Copy link

TH-Soft commented Jul 7, 2023

There is a callback that allows you to display the progress of the preparation. I don't know how to stop the preparation from within the callback. Not sure if throwing an exception is enough to stop preparation.
Another approach: create a thread to do the preparation and kill the thread and the document when user wants to cancel rendering.

Link to sample code in the support forum (shows progress, but does not yet support cancellation):
https://forum.pdfsharp.net/viewtopic.php?f=8&t=3172

@heku
Copy link
Author

heku commented Jul 7, 2023

Yes, wrap a thread is my current workaround, but it is better to add a method or parameter to make it cancelable, because thread.abort is not recommend as it's unsafe, and recent .net core even drop support for it.

My proposal is, add a new method PrepareRenderPages(CancellationToken token), and check the token inside via token.ThrowIfCancellationRequested() is enough I think.

@ThomasHoevel ThomasHoevel added enhancement New feature or request wontfix This will not be worked on and removed wontfix This will not be worked on enhancement New feature or request labels Jul 11, 2023
@ThomasHoevel
Copy link
Member

I tested it: an exception thrown in PrepareDocumentProgress stops the preparation progress.
Thus preparation can easily be cancelled by throwing an exception.
Rendering can easily be stopped if you render page by page.

If you want to use a CancellationToken, just check it inside the callback and in the loop that renders the document.

@heku
Copy link
Author

heku commented Jul 14, 2023

it works. thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants