We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Interrupt document conversion process using C#

Hi @tahir.manzoor
The InterruptionToken class is marked as Obsolete in Aspose.Words v20.9.0, so it’s not a good idea to use it any more :-(. The suggested alternate is to use IPageLayoutCallback, but this doesn’t look to be a like-for-like replacement, and it also doesn’t look like it’s possible to use it in the following code:

var doc = new Aspose.Words.Document(sourceStream);
var saveOptions = new PdfSaveOptions();
using (var pdfStream = GetStream())
{
    // Processing hangs in the call to Save below
    doc.Save(pdfStream, saveOptions);
}

I’ve tried using the IPageSavingCallback as follows:

public class WordToPdf :  Aspose.Words.Saving.IPageSavingCallback
{
    public void DoConvert(Stream sourceStream)
    {
        var doc = new Aspose.Words.Document(sourceStream);
        var saveOptions = new PdfSaveOptions();
        saveOptions.PageSavingCallback = this;

        using (var pdfStream = GetStream())
        {
            doc.Save(pdfStream, saveOptions);
        }
    }

    void IPageSavingCallback.PageSaving(PageSavingArgs args)
    {
        // This method never gets called
    }
}

…but the PageSaving method never gets called, and the process still hangs in the Save method.
Am I missing something?

I might have cracked it! Here’s an extract of my code, using IPageLayoutCallback

public class WordToPdf :  Aspose.Words.Layout.IPageLayoutCallback
{
    private readonly System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
    private readonly TimeSpan _timeout = TimeSpan.FromSeconds(30);

    public void DoConvert(Stream sourceStream)
    {
        var doc = new Aspose.Words.Document(sourceStream);
        doc.LayoutOptions.Callback = this;

        using (var pdfStream = GetStream())
        {
            try
            {
                doc.Save(pdfStream, new PdfSaveOptions());
            }
            catch (MyTimeoutException)
            {
                // oh no! The save timed-out
            }
        }
    }

    void IPageLayoutCallback.Notify(PageLayoutCallbackArgs args)
    {
        // Ensure Started
        if (!this._stopwatch.IsRunning)
        {
            this._stopwatch.Start();
        }
        
        // Check Timeout
        if (this._stopwatch.Elapsed > this._timeout)
        {
            this._stopwatch.Stop();
            throw new MyTimeoutException($"The conversion timed-out after {this._timeout.TotalSeconds} seconds.");
        }
    }
}

@Weechap

Yes, your understanding about IPageLayoutCallback is correct. It is nice to hear from you that your issue has been solved.