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.
///
/// 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,
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,
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,
Hi Karrim,
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.