Add API to Interrupt Word to PDF conversion when it takes time using .NET

@tahir.manzoor Some of the other aspose products have had timeout code (Aspose.Email for .NET 20.7 Release Notes|Documentation) added to prevent this zombie state. I see hanging issues on multiple threads in your forums of lots of your products so surely adding the same timeout code will help.
We’ve got hanging aspose code when saving Word to PDF for which I’ll create a new thread but it would be such a better customer experience if this timeout code could be added throughout the product on all the save methods, which is where the majority of hanging problems seems to occur.

@Drammy

You can use InterruptionToken class instance and bind it to the current thread executing Aspose.Words code. Then calling Interrupt on the token instance should break the execution.

Please use the following code example to achieve your requirement.

using System.Threading;
using Aspose.Words;
 
namespace conversion
{
    /// <summary>
    /// My Aspose document.
    /// </summary>
    public class MyDocument : Aspose.Words.Document
    {
        public MyDocument(string fileName) : base(fileName) {}
 
        public bool TryToSave(string fileName, int timeout)
        {
            InterruptionToken token = new InterruptionToken();
 
            bool finished = SaveWithTimeout(token,
                () =>
                {
                    token.BindToCurrentThread();
                    Save(fileName);
                }, timeout);
 
            return finished;
        }
 
        private bool SaveWithTimeout(InterruptionToken token, ThreadStart threadStart, int timeout)
        {
            Thread workerThread = new Thread(threadStart);
            workerThread.Start();
 
            bool finished = workerThread.Join(timeout);
            if (!finished)
            {
                token.Interrupt();
            }
            return finished;
        }
    }
}

Sample usage is as follows:

MyDocument myDoc = new MyDocument(@"C:\in.docx");
bool done = myDoc.TryToSave(@"C:\output.pdf", 60000);
Console.WriteLine(done ? "Converted" : "Interrupted by timeout.");

OK thanks @tahir.manzoor; we’ll give this a try

@Drammy

Further to my previous post, the InterruptionToken class is now obsolete.In your case, we suggest you please implement IPageLayoutCallbac interface to have your own custom method called during build and rendering of page layout model.

We have added this feature in Aspose.Words 20.5. Please check the release notes from here:
Aspose.Words for .NET 20.5 Release Notes

Hello, with the IPageLayoutCallback i am not able to interrupt the saving process from the outside, right? If yes, how can i do this now?

Kind Regards,
Andy

@AStelzner You can interrupt the layout process. For example the following code interrupts the layout process when layout of two first pages is built:

int pagesToRender = 2;
Document doc = new Document("C:\\Temp\\in.docx");
doc.LayoutOptions.Callback = new PageLayoutCallback(pagesToRender);
try
{
    doc.UpdatePageLayout();
}
catch (PageLimitReachedException ex)
{
    PdfSaveOptions opt = new PdfSaveOptions();
    opt.PageSet = new PageSet(new PageRange(0, pagesToRender - 1));
    doc.Save("C:\\Temp\\out.pdf", opt);
}
private class PageLayoutCallback : IPageLayoutCallback
{
    public PageLayoutCallback(int maxPageCount)
    {
        mMaxPageCount = maxPageCount;
    }

    public void Notify(PageLayoutCallbackArgs args)
    {
        if (args.Event == PageLayoutEvent.PartReflowStarted &&
                args.PageIndex >= mMaxPageCount)
            throw new PageLimitReachedException();
    }

    private int mMaxPageCount;
}
private  class PageLimitReachedException : Exception { }

In the Notify method you can use other condition for the layout process interruption. For example check the flag set by the outside event.

Hello,

sorry, but i don´t understand the approach!

The doc.Save(“C:\Temp\out.pdf”, opt) call is in the catch block, but i want to interrupt this call from the outside!! For example the user wants to start a Word to PDF conversion which takes a long time and want to cancel it before its done! This is not possible with this approach, right?

Why there is not a meaningful and simple successor of the InterruptionToken???

Kind Ragards,
Andy

@AStelzner As I mentioned you can define a flag - an interruption token, which is available to both your external event and your IPageLayoutCallback implementation. Then in the Notify method you can use a condition that checks this flag for the layout process interruption.
The code example provided above just uses number of pages as an interruption limit.

1 Like