Support For Interruptable Library

Interruptable Library

The InterruptionToken and InterruptionTokenSource classes have been added to Aspose.Slides for C++. These types support interruption of long-running tasks, such as deserialization, serialization or rendering. The InterruptionTokenSource represents the source of the token or multiple tokens passed to the LoadOptions.set_InterruptionToken() method. When the interruption token is set and the LoadOptions instance passed to the Presentation constructor, any long-running task related to this presentation will be interrupted when the InterruptionTokenSource.Interrupt() method will be invoked.

The code snippet below demonstrates interrupting a running task.

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

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

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

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

        SharedPtr<Presentation> presentation = System::MakeObject<Presentation>(dataDir + u"pres.pptx", options);
        presentation->Save(dataDir + u"pres.ppt", Export::SaveFormat::Ppt);
    });
    auto action = System::Action<SharedPtr<IInterruptionToken>>(function);

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