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. Sample inherited class code is as follows:
using System.Threading;
using Aspose.Words;
namespace MyCompany
{
/// <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:\BigDoc.docx");
bool done = myDoc.TryToSave(@"C:\BigDoc.pdf", 60000);
Console.WriteLine(done ? "Converted" : "Interrupted by timeout.");
I am afraid, in Aspose.Words for Java we do not have InterruptionToken class because Aspose.Words for Java relies on Java’s Thread functionality. The Java code will look something like this:
public void testThreadInterruption() throws InterruptedException
{
// run Aspose.Words long operation in a separate thread
Thread infiniteThread = new Thread(infiniteProcessingLoop());
infiniteThread.start();
// interrupt it in a while
Thread.sleep(2000);
infiniteThread.interrupt();
// wait till it dies
infiniteThread.join(5000);
// check if it is dead
if (infiniteThread.getState() == Thread.State.RUNNABLE)
Assert.fail("Thread didn't stop properly");
}
private static Runnable infiniteProcessingLoop()
{
return new Runnable()
{
@Override
public void run()
{
try
{
while (true)
{
new Document("pathToTestDoc").updatePageLayout();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
};
}
I noticed it saves the Excel file too. We will check it out soon.
For the time being could you add a line to your code segment, it should work fine:
e.g Sample code:
…
startTime = System.currentTimeMillis(); workbook.save(pdfFilePath, FileFormatType.PDF);//add a variable to specify the .pdf file.
workbook.save(dstFilePath, FileFormatType.XLSX);
System.out.println(“finish workbook.save!”);
…
A corrupted PDF file might be created with less file size. The XLSX file will not be created.
We have evaluated the issue regarding saving XLSX file in interruption process. We need to improve the relevant internal modules by adding more check points (higher frequency) while saving the Excel files. I have logged a ticket with an id “CELLSJAVA-42677” for your issue. We will look into it soon.
Once we have an update on it, we will let you know here.
This is to inform you that we have fixed your issue (logged earlier as “CELLSJAVA-42677”) now. We will soon provide you the fixed version after performing QA and incorporating other enhancements and fixes.
The issues you have found earlier (filed as CELLSJAVA-42677) have been fixed in Aspose.Cells for Java 18.8. You can also get the latest Aspose.Cells for Java version from Maven repos. with simple configurations. Please see the document for your reference: Installation|Documentation
This message was posted using BugNotificationTool from Downloads module by Amjad_Sahi