Skip to content

Commit 2cebd8e

Browse files
committed
Added cancellation documentation page
1 parent d75f17a commit 2cebd8e

2 files changed

Lines changed: 122 additions & 1 deletion

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
deploy_key
2-
.DS_Store
2+
.DS_Store
3+
.idea/
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
id: cancellation
3+
url: conversion/net/cancellation
4+
title: Cancellation
5+
linkTitle: Cancellation
6+
weight: 6
7+
description: "Learn how to cancel or set a timeout for long-running document conversions using CancellationToken in GroupDocs.Conversion for .NET API."
8+
keywords: Cancel conversion, timeout conversion, CancellationToken, cancel long-running conversion, conversion timeout
9+
productName: GroupDocs.Conversion for .NET
10+
hideChildren: False
11+
---
12+
13+
[**GroupDocs.Conversion**](https://products.groupdocs.com/conversion/net) supports cancellation of long-running conversion operations using the standard .NET [CancellationToken](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtoken) mechanism. This is particularly useful when converting large or complex documents where you need to:
14+
15+
* Set a maximum time limit for the conversion operation
16+
* Allow users to cancel an ongoing conversion
17+
* Implement responsive applications that can abort conversions on demand
18+
19+
## Using CancellationToken with classic syntax
20+
21+
To cancel or timeout a conversion operation, follow these steps:
22+
23+
1. Create a [CancellationTokenSource](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource) instance.
24+
2. Optionally, configure automatic cancellation after a specified time using the `CancelAfter` method.
25+
3. Create an instance of the [Converter](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/converter) class and pass the source document path as the constructor parameter.
26+
4. Instantiate the appropriate [ConvertOptions](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.options.convert/convertoptions) class (e.g., [PdfConvertOptions](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.options.convert/pdfconvertoptions), [WordProcessingConvertOptions](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.options.convert/wordprocessingconvertoptions), etc.).
27+
5. Call the [Convert](https://reference.groupdocs.com/conversion/net/groupdocs.conversion/converter/convert/#convert_3) method with the `CancellationToken` parameter.
28+
6. Handle the `OperationCanceledException` to gracefully manage cancellation scenarios.
29+
30+
The following code snippet shows how to set a timeout for a conversion operation:
31+
32+
```csharp
33+
using GroupDocs.Conversion;
34+
using GroupDocs.Conversion.Options.Convert;
35+
36+
using (Converter converter = new Converter("sample.dwg"))
37+
{
38+
using (CancellationTokenSource cts = new CancellationTokenSource())
39+
{
40+
// Auto-cancel after 60 seconds
41+
cts.CancelAfter(TimeSpan.FromSeconds(60));
42+
43+
try
44+
{
45+
PdfConvertOptions options = new PdfConvertOptions();
46+
converter.Convert("converted.pdf", options, cts.Token);
47+
}
48+
catch (OperationCanceledException)
49+
{
50+
Console.WriteLine("Conversion was cancelled or timed out");
51+
}
52+
}
53+
}
54+
```
55+
56+
## Using CancellationToken with fluent syntax
57+
58+
You can also use [fluent syntax]({{< ref "conversion/net/developer-guide/basic-usage/fluent-syntax.md" >}}) to perform cancellable conversions.
59+
60+
The following code snippet shows how to use CancellationToken with fluent syntax:
61+
62+
```csharp
63+
using GroupDocs.Conversion;
64+
using GroupDocs.Conversion.Options.Convert;
65+
66+
using (CancellationTokenSource cts = new CancellationTokenSource())
67+
{
68+
// Auto-cancel after 60 seconds
69+
cts.CancelAfter(TimeSpan.FromSeconds(60));
70+
71+
try
72+
{
73+
FluentConverter
74+
.Load("sample.dwg")
75+
.ConvertTo("converted.pdf")
76+
.WithOptions(new PdfConvertOptions())
77+
.Convert(cts.Token);
78+
}
79+
catch (OperationCanceledException)
80+
{
81+
Console.WriteLine("Conversion was cancelled or timed out");
82+
}
83+
}
84+
```
85+
86+
## Manual cancellation
87+
88+
You can also trigger cancellation manually, for example, in response to a user action:
89+
90+
```csharp
91+
using GroupDocs.Conversion;
92+
using GroupDocs.Conversion.Options.Convert;
93+
94+
CancellationTokenSource cts = new CancellationTokenSource();
95+
96+
// Start conversion in a background task
97+
Task conversionTask = Task.Run(() =>
98+
{
99+
using (Converter converter = new Converter("large-document.dwg"))
100+
{
101+
try
102+
{
103+
PdfConvertOptions options = new PdfConvertOptions();
104+
converter.Convert("converted.pdf", options, cts.Token);
105+
Console.WriteLine("Conversion completed successfully");
106+
}
107+
catch (OperationCanceledException)
108+
{
109+
Console.WriteLine("Conversion was cancelled by user");
110+
}
111+
}
112+
});
113+
114+
// Simulate user cancellation after 10 seconds
115+
Thread.Sleep(TimeSpan.FromSeconds(10));
116+
cts.Cancel();
117+
118+
await conversionTask;
119+
cts.Dispose();
120+
```

0 commit comments

Comments
 (0)