Cancel rendering

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

Since the 21.7 version, GroupDocs.Viewer supports task cancellation using the cancellation token.

To cancel a task, follow these steps:

  1. Create the CancellationTokenSource object.
  2. Create the CancellationToken object.
  3. Create the Task object. When creating, specify the cancellation token.
  4. Cancel the task.

You can cancel tasks using one of the following methods:

  • To cancel the task in a specified time, call the CancelAfter method.
  • To cancel the task at any time, call the Cancel method.

The following code snippet shows how to cancel a task:

using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
// ...

// Create cancellation token source.
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.Token;

// Create task and pass token
Task runTask = Task.Run(() =>
{
    using (Viewer viewer = new Viewer("sample.docx"))
    {
        HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources();
        options.RenderComments = true;
        viewer.View(options, cancellationToken);
    }
}, cancellationToken);

// Cancel task after 1000 ms.
cancellationTokenSource.CancelAfter(1000);

// Also you can call Cancel method at any time
//cancellationTokenSource.Cancel();

// Wait for the task to cancel.
Thread.Sleep(2000);

Debug.Assert(runTask.IsCanceled);
Imports System.Threading
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
' ...

Module Program
    Sub Main(args As String())
        ' Create cancellation token source.
        Dim cancellationTokenSource As CancellationTokenSource = New CancellationTokenSource()
        Dim cancellationToken As CancellationToken = cancellationTokenSource.Token

        ' Create task and pass token
        Dim runTask As Task = Task.Run(Sub()
                                           Using viewer As Viewer = New Viewer("sample.docx")
                                               Dim options As HtmlViewOptions = HtmlViewOptions.ForEmbeddedResources()
                                               options.RenderComments = True
                                               viewer.View(options, cancellationToken)
                                           End Using
                                       End Sub, cancellationToken)

        ' Cancel task after 1000 ms.
        cancellationTokenSource.CancelAfter(1000)

        ' Also you can call Cancel method at any time
        'cancellationTokenSource.Cancel();

        ' Wait for the task to cancel.
        Thread.Sleep(2000)

        Debug.Assert(runTask.IsCanceled)
    End Sub
End Module

sample.docx is the sample file used in this example. Click here to download it.

Note

If runTask.IsCancelled is true, then the task has been canceled.

To properly handle task cancellation, pass a cancellation token to the Task.Run method. Otherwise, runTask.IsCancelled is not true.

The following methods of the Viewer class also support cancellation:

You can also view the example in our public GitHub repository.