Open and Save Cancellation

Hello

Sometimes we convert very large files which can take hours to complete the largest time is the initial load, and second is the actual save when converting.

we would like an event on the Document class to cancel an open or a save this would stop us hiving to use Thread.Abort();

something similar to:

var doc = new Document();
doc.Openprogess += OnOpenprogress;
doc.Open("verylargefile.docx");

void OnOpenprogress(sender, OpenArgs arg)
{
    if (UserHasCancelled)
    {
        arg.Cancel = true;
    }
}

Hi Karrim,

Thanks for your inquiry. Thread.Abort method is unsafe way for interrupt Aspose.Words execution. For example, file descriptor may not be closed. Aspose.Words has special interruption way using class InterruptionToken. Please use following code examples to achieve your requirements.

Hope this helps you. Please let us know if you have any more queries.

/// 
/// My Aspose document.
/// 
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();
            try
            {
                Save(fileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Interrupted");
            }
        }, 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;
    }
}
MyDocument myDoc = new MyDocument(MyDir + "in.docx");
bool done = myDoc.TryToSave(MyDir + "Out.docx", 1000);
Console.WriteLine(done? "Converted" : "Interrupted by timeout.");

That is great thanks, is there any way to Interrupt the Open,

: base(filename)

can take an hour or longer for us.

Hi Karrim,

Thanks for your inquiry. If a document takes an hour to load into Aspose.Words DOM, this might be an issue. Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

Hello

Here is a smaller example file similar to the files we convert to pdf Microsoft word can open this file and will only use 200mb of memory but Aspose will use 4bg I guess the maximum it can, to open this file takes 33 seconds and to convert it takes 2 minutes so you can see an open cancel will be very useful otherwise we will have to continue to use the thread abort during the opening stage to cancel this conversion.

as I said this a a smaller file we can get files 10 to 20 times bigger than this one, so an open can take many minutes, we understand the time as they are large files just need a safe way to cancel.

Hi Karrim,

Thanks for sharing the detail.

In terms of memory, Aspose.Words does not have any limitations. If you’re loading huge Word documents into Aspose.Words’ DOM, more memory would be required. This is because during processing, the document needs to be held wholly in memory. Usually, Aspose.Words needs 10 times more memory than the original document size to build a DOM in the memory.

The process of building layout model is not linear; it may take a minute to render one page and may take a few seconds to render 100 pages. Also, Aspose.Words has to create APS (Aspose Page Specification) model in memory and this may again eat some more time for some documents. Rest assured, we’re always working on improving performance; but, rendering will be always running slower than simple saving to flow formats (e.g doc/docx).

At the moment only interruption when rendering to fixed-page formats such as PDF, XPS, image is supported. Could you please share some detail about your Use Case in which you want to stop loading of document into Aspose.Words DOM? We will then log this feature in our issue tracking system.

Hello

We have a document management wcf windows service that may hold mail merged docx files with potentially 50,000+ pages.

Front end user requests the file in PDF format, so the service will use aspose on a background thread to open and save this file as a pdf. The user will see a progress indicator and a complete open and save may take an hour or so depending on what else is going on in that server at the time.

One minute in the user decides they don’t want to wait so they hit cancel, at that moment the background thread is in the process of opening the document,

var document = new Aspose.Words.Document("verylargemailmerge.docx");

Can we please send the thread an interrupt at the point where the user wants to cancel rather than waiting potentially many minutes plus the drain on memory and CPU for the document to open and exit the thread correctly.

Regards

Karrim

Hi Karrim,

Thanks for sharing the detail. We have logged a feature request as WORDSNET-12795 to interrupt loading of document into Aspose.Words DOM in our issue tracking system. Our product team will look into the possibility of implementation of this feature. Once we have any information about this feature, we will update you via this forum thread.

Please let us know if you have any more queries.

Hi Karrim,

Thanks your patience. There is a native .NET way of achieving your requirement. It is wrapping Aspose.Words operations into a Task with a cancellation token that can be revoked at any time. Please read about canceling a task from following link.

How to: Cancel a Task and Its Children

Hope this helps you. Please let us know if you have any more queries.

Yes this would be great if aspose supported a CancellationToken on open or saving then the cancel task solution would work but it does not. This means the child tasks will sit there waiting for aspose to finish its work before it can cancel which is the same problem, as each task could be consuming gigabytes of ram.

Hi Karrim,

Thanks for your inquiry. We have logged the shared detail in our issue tracking system. Once there is any update available on this feature request we will update you via this forum thread.

Please let us know if you have any more queries.

Hi Karrim,

Thanks for your patience. It is to update you that we have closed the issue (WORDSNET-12795) with ‘‘Won’t Fix’’ resolution. It is a problem for us to use Cancellation Tokens due to framework versions limitations.

However there is a simple solution that can be used with threads. Please use InterruptionToken class instance and bind it to the current thread executing Aspose.Words code. Then calling Interrupt on the token instance will break the execution.