Free Support Forum - groupdocs.com

Cancel Viewer document loading / document rendering (.NET)

Hi.

I use “GroupDocs.Viewer 20.3.0 for .NET” library to render various documents, passing data as System.Stream to Viewer and rendering its data into HTML.

Viewer processes streams in a separate worker thread using System.Task to prevent blocking GUI when document is being rendered. I’m facing problems with some data which is being processed for a long time.

For example, 200Mb archive file passed as a Stream to GroupDocs.Viewers.Viewer are being processed for a couple of minutes.

I want to cancel current stream loading / rendering if user has chosen another document while the current one is still being processing by the Viewer.

Is there any possibility to cancel stream loading stage or data rendering stage on GroupDocs.Viewers.Viewer side (for example by passing CancellationToken) or how this cancellation operation can be achieved?

2 Likes

@paraben,

Currently there is no such feature to stop rendering or loading of the document at API level. You can use Task Cancellation process to achieve this.
However, we are further investigating this scenario. Your investigation ticket ID is VIEWERNET-2431. As there’s any update, you’ll be notified.
Let us share the possible workaround:

Safe cancellation:
Please check the attached sample_app.zip (74.7 KB) that demonstrates how to cancel processing safely. It works when rendering to HTML/PNG/JPG. Try running the application and press Enter at some stage and you should get something like console_output.png (157.7 KB). At the moment the only way to cancel safely that I see is in the CreatePageStream which is called when we need a stream to write page contents to. Unfortunately, there is no way to safely cancel processing on the stage when document is being opened. So you have to wait until a document opened and only then you can cancel.

Unsafe cancellation:
The unsafe method would be to abort the thread but we would not recommend aborting because it may happen that thread is aborted when FileStream is initializing and if you abort the thread that file will be kept open until the end of app domain.

Thanks for your reply.

The issue was resolved by creation a wrapper for Stream, which contains CancellationToken.
This Stream wrapper is returned by a delegate, which is passed into Viewer constructor.
The CancellationToken state is checked in wrapper’s Stream.Read method and OperationCanelledException is thrown if the cancellation was requested.

1 Like

@paraben,

Good to know that your issue is resolved.