Cancellation

GroupDocs.Conversion supports cancellation of long-running conversion operations using the standard .NET CancellationToken mechanism. This is particularly useful when converting large or complex documents where you need to:

  • Set a maximum time limit for the conversion operation
  • Allow users to cancel an ongoing conversion
  • Implement responsive applications that can abort conversions on demand

Using CancellationToken with classic syntax

To cancel or timeout a conversion operation, follow these steps:

  1. Create a CancellationTokenSource instance.
  2. Optionally, configure automatic cancellation after a specified time using the CancelAfter method.
  3. Create an instance of the Converter class and pass the source document path as the constructor parameter.
  4. Instantiate the appropriate ConvertOptions class (e.g., PdfConvertOptions, WordProcessingConvertOptions, etc.).
  5. Call the Convert method with the CancellationToken parameter.
  6. Handle the OperationCanceledException to gracefully manage cancellation scenarios.

The following code snippet shows how to set a timeout for a conversion operation:

using GroupDocs.Conversion;
using GroupDocs.Conversion.Options.Convert;

using (Converter converter = new Converter("sample.dwg"))
{
    using (CancellationTokenSource cts = new CancellationTokenSource())
    {
        // Auto-cancel after 60 seconds
        cts.CancelAfter(TimeSpan.FromSeconds(60));

        try
        {
            PdfConvertOptions options = new PdfConvertOptions();
            converter.Convert("converted.pdf", options, cts.Token);
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("Conversion was cancelled or timed out");
        }
    }
}

Using CancellationToken with fluent syntax

You can also use fluent syntax to perform cancellable conversions.

The following code snippet shows how to use CancellationToken with fluent syntax:

using GroupDocs.Conversion;
using GroupDocs.Conversion.Options.Convert;

using (CancellationTokenSource cts = new CancellationTokenSource())
{
    // Auto-cancel after 60 seconds
    cts.CancelAfter(TimeSpan.FromSeconds(60));

    try
    {
        FluentConverter
            .Load("sample.dwg")
            .ConvertTo("converted.pdf")
            .WithOptions(new PdfConvertOptions())
            .Convert(cts.Token);
    }
    catch (OperationCanceledException)
    {
        Console.WriteLine("Conversion was cancelled or timed out");
    }
}

Manual cancellation

You can also trigger cancellation manually, for example, in response to a user action:

using GroupDocs.Conversion;
using GroupDocs.Conversion.Options.Convert;

CancellationTokenSource cts = new CancellationTokenSource();

// Start conversion in a background task
Task conversionTask = Task.Run(() =>
{
    using (Converter converter = new Converter("large-document.dwg"))
    {
        try
        {
            PdfConvertOptions options = new PdfConvertOptions();
            converter.Convert("converted.pdf", options, cts.Token);
            Console.WriteLine("Conversion completed successfully");
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("Conversion was cancelled by user");
        }
    }
});

// Simulate user cancellation after 10 seconds
Thread.Sleep(TimeSpan.FromSeconds(10));
cts.Cancel();

await conversionTask;
cts.Dispose();
Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.