Create the Task object. When creating, specify the cancellation token.
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:
// Create cancellation token source.CancellationTokenSourcecancellationTokenSource=newCancellationTokenSource();// Create (get) cancellation token object.CancellationTokencancellationToken=cancellationTokenSource.Token;// Create task and pass tokenTaskrunTask=Task.Run(()=>{using(Viewerviewer=newViewer(TestFiles.SAMPLE_DOCX,newViewerSettings(newGroupDocs.Viewer.Logging.ConsoleLogger()))){HtmlViewOptionsoptions=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);// runTask.IsCanceled == true
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: