Support For Interruptable Library
Interruptable Library
In Aspose.Slides 18.4, we introduced the InterruptionToken and InterruptionTokenSource classes. They allow you to interrupt long-running tasks such as deserialization, serialization, and rendering.
- InterruptionTokenSource is the source of the token(s) passed to ILoadOptions.InterruptionToken.
- When ILoadOptions.InterruptionToken is set and the LoadOptions instance is passed to the Presentation constructor, invoking InterruptionTokenSource.Interrupt() interrupts any long-running task associated with that Presentation.
The following code snippet demonstrates interrupting a running task:
public static void Run()
{
Action<IInterruptionToken> action = (IInterruptionToken token) =>
{
LoadOptions options = new LoadOptions { InterruptionToken = token };
using (Presentation presentation = new Presentation("sample.pptx", options))
{
presentation.Save("sample.ppt", SaveFormat.Ppt);
}
};
InterruptionTokenSource tokenSource = new InterruptionTokenSource();
Run(action, tokenSource.Token); // run the action in a separate thread
Thread.Sleep(10000); // timeout
tokenSource.Interrupt(); // stop the conversion
}
private static void Run(Action<IInterruptionToken> action, IInterruptionToken token)
{
Task.Run(() => { action(token); });
}
.NET CancellationToken and Interruptable Library
When you need to use a CancellationToken alongside the Aspose.Slides Interruptible library, wrap the Presentation processing and interrupt the InterruptionToken when CancellationToken.IsCancellationRequested is true
.
This C# code demonstrates the operation:
public static void Main()
{
CancellationTokenSource tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(20));
ProcessPresentation("sample.pptx", "sample.pdf", tokenSource.Token);
}
static void ProcessPresentation(string path, string outPath, CancellationToken cancellationToken)
{
Action<IInterruptionToken> action = (IInterruptionToken token) =>
{
LoadOptions options = new LoadOptions {InterruptionToken = token};
using (Presentation presentation = new Presentation(path, options))
{
presentation.Save(outPath, SaveFormat.Pdf);
}
};
InterruptionTokenSource tokenSource = new InterruptionTokenSource();
Task task = Run(action, tokenSource.Token); // run the action in a separate thread
while (!task.Wait(500)) // wait and monitor whether cancellationToken.IsCancellationRequested is set
{
if (cancellationToken.IsCancellationRequested)
{
Console.WriteLine("Presentation processing was canceled");
tokenSource.Interrupt(); // interrupt Presentation processing
}
}
}
private static Task Run(Action<IInterruptionToken> action, IInterruptionToken token)
{
return Task.Run(() =>
{
action(token);
});
}
FAQ
Q: What is the purpose of the Aspose.Slides interrupt library?
It provides a mechanism to interrupt long-running operations—such as loading, saving, or rendering presentations—before they complete. This is useful when processing time must be limited or the task is no longer needed.
Q: What is the difference between InterruptionToken and InterruptionTokenSource?
InterruptionToken
is passed to the Aspose.Slides API and checked during long-running operations.InterruptionTokenSource
is used in your code to create tokens and trigger interruptions by callingInterrupt()
.
Q: Can I use .NET CancellationToken with the interrupt library?
Yes. You can monitor the CancellationToken in your application logic and call InterruptionTokenSource.Interrupt() when cancellation is requested. This enables Aspose.Slides to integrate with standard .NET cancellation workflows.
Q: What tasks can be interrupted?
Any Aspose.Slides task that accepts an InterruptionToken—such as loading a presentation with Presentation(path, loadOptions)
or saving with Presentation.Save(...)
—can be interrupted.
Q: Does interruption happen immediately?
No. Interruption is cooperative: the operation periodically checks the token and stops as soon as it detects that Interrupt() has been called.
Q: What happens if I call Interrupt() after a task has already completed?
Nothing—the call has no effect if the corresponding task has already completed.
Q: Can I reuse the same InterruptionTokenSource for multiple tasks?
Yes—but after you call Interrupt() on that source, all tasks using its tokens will be interrupted. Use separate token sources to manage tasks independently.