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.

The following code snippet demonstrates interrupting a running task:

void Run(Action<SharedPtr<IInterruptionToken>> action, SharedPtr<IInterruptionToken> token)
{
    auto threadFunction = std::function<void()>([&action, &token]() -> void
    {
        action(token);
    });

    auto thread = System::MakeObject<Threading::Thread>(threadFunction);
    thread->Start();
}

void Run()
{
    String dataDir = GetDataPath();

    auto function = std::function<void(SharedPtr<IInterruptionToken> token)> ([&dataDir](SharedPtr<IInterruptionToken> token) -> void
    {
        auto options = System::MakeObject<LoadOptions>();
        options->set_InterruptionToken(token);

        auto presentation = System::MakeObject<Presentation>(dataDir + u"sample.pptx", options);
        presentation->Save(dataDir + u"sample.ppt", Export::SaveFormat::Ppt);
    });

    auto action = System::Action<SharedPtr<IInterruptionToken>>(function);
    auto tokenSource = System::MakeObject<InterruptionTokenSource>();
    
    Run(action, tokenSource->get_Token()); // run the action in a separate thread
    Threading::Thread::Sleep(10000);       // timeout
    tokenSource->Interrupt();              // stop the conversion
}

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 calling Interrupt().

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.