Stop conversion if conversion taking more than specified time

Hi,

I am using aspose for MS document conversion to PDF/HTML.
Is there any way we can specify a timeout limit i.e. let’s say if conversion is taking more than 3 minutes for any document (doc(x), xls(x), ppt(x)) then aspose must cancel the conversion process.

@poojard,

Thanks for contacting support.

Aspose.Words provides the capabilities to create as well as manipulate MS Word files and as per your requirements, it supports the request feature when converting DOC/DOCX files to PDF/HTML format. Aspose.Words has special interruption way using class InterruptionToken. Please use following code examples to achieve your requirements. 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.pdf", 1000);
Console.WriteLine(done ? "Converted" : "Interrupted by timeout.");

Now concerning to MS Excel worksheets, please note that Aspose.Cells provides the capabilities to create as well as manipulate MS Excel files. It also supports the feature to timeout when performing XLS/XLSX files to PDF/HTML format. For further details, please visit Interuptable library.

However I am afraid currently there is no such support availble in Aspose.Slides to get elapsed time for exporting presentaiton to PDF/HTML and making some decision. We have added this feature request with ID SLIDESNET-39341 as investigation in our bug tracking system and will keep you updated on the status of correction. Please be patient and spare us little time.

Thanks for the response.
Can you please share the code in Java for word conversion or may be you can share the link for reference

@poojard,

We are working on creating the required code snippet based on Aspose.Words for Java and will get back to you soon.

Thanks for the response.

Can you just help with me with classes and their details for the same. Since this is kind of blocker for us. And it’s very critical to fix this at priority.

@poojard,

Thanks for your patience.

I have further discussed the details with related team and I am afraid currently Aspose.Words do not support the requested feature. The earlier shared approach was a workaround for Aspose.Words for .NET and meanwhile we already logged a feature request as WORDSNET-11575 to stop Document.Save method after specific time. Once the feature is implemented, we will port the same feature to its Java sibling. Please be patient and spare su little time. We are sorry for this delay and inconvenience.

Okay.
Thanks you. Will find some alternative for this particular issue

The issues you have found earlier (filed as SLIDESNET-39341) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by mudassir.fayyaz

Is the issue WORDSNET-11575 regarding stopping the Document.Save method already implemented in .NET? If not, do you know when this will be available?

Also, if it is not available yet, do you know a workaround? I would like to stop the save method after 5 seconds.

Is the same feature planned for Aspose.Cells for .NET, and do you know a workaround for that too?

@dhmderuiter

Thanks for using Aspose APIs.

For Aspose.Cells, please use Workbook.InterruptMonitor property. InterruptMonitor is useful when loading or saving the workbook, is taking too much time. You will need two threads for it. One thread will load or save the workbook and other thread will monitor and interrupt the loading or saving of workbook.

C#

InterruptMonitor im = new InterruptMonitor();

//This function is in main thread
void LoadWorkbook()
{
	LoadOptions opts = new LoadOptions();
	opts.InterruptMonitor = im; //<<<<<<<<<<<<<<<<<

	Workbook wb = new Workbook("Large.xlsx", opts);
	wb.InterruptMonitor = im;//<<<<<<<<<<<<<<<<<
	
	wb.Save("output.xlsx");
	
}

//This function is in another thread.
void WaitForWhileAndThenInterrupt()
{
	//Wait for a while and then interrupt
	im.Interrupt();            
}

@dhmderuiter,

Thanks for your inquiry.

We regret to share with you that WORDSNET-11575 has been postponed. I am afraid, there is no estimate available at the moment. We will inform you via this forum thread as soon as there are any further developments. We apologize for your inconvenience.

The workaround already shared in this forum thread. Please check the following link.

Thanks for the help. Regarding the InterruptMonitor in Aspose.Cells, I don’t completely understand how to use it. Do you have some example code which interrupts the process after for example 3 seconds?

Will the interrupt throw an exception?

@dhmderuiter

Please spare us some time, we will update you tomorrow hopefully.

@dhmderuiter

Thanks for using Aspose APIs.

Please download and try this sample code that explains how to use InterruptMonitor object. The code converts quite a large Excel file to Pdf. It will take several seconds (i.e. more than 30 seconds) to get it converted because of these lines of code.

//Access cell J1000000 and add some text inside it.
Cell cell = ws.Cells["J1000000"];
cell.PutValue("This is text.");

As you see J1000000 is quite a farther cell in XLSX file. However, the WaitForWhileAndThenInterrupt() method interrupts the conversion after 10 seconds and program ends/terminates.

Please use the following code to execute the sample code.

TestInterruptMonitor tim = new TestInterruptMonitor();
tim.Run();

Download Link:
Sample Code.zip (743 Bytes)


Reference Article:

Thanks, I’m trying to download the sample code, but I get the message ‘Sorry, this file is private. Only visible to topic owner and staff members’. Could you attach it in a different way or send it to my email?

@dhmderuiter

I have sent the sample code to your email. Besides, you can find the entire sample code in this article.

Thanks, I have tried the code, but the interrupt does not seem to be working. I’m using the following code:

Private MyInterruptMonitor As New Aspose.Cells.InterruptMonitor

Private Sub RunPdfFromExcelCreator(DownloadLocatie As String, PDFDownloadLocatie As String)
  Dim Thread1 As New Thread(Sub()
    Dim File As New Aspose.Cells.Workbook(DownloadLocatie)
    File.InterruptMonitor = MyInterruptMonitor
    File.Save(PDFDownloadLocatie)
  End Sub)
  Thread1.Start()

  Dim Thread2 As New Thread(Sub()
    Thread.Sleep(1000 * 5)
    MyInterruptMonitor.Interrupt()
  End Sub)
  Thread2.Start()

  Thread1.Join()
  Thread2.Join()
End Sub

Please see attached the file I’m trying to use it with. This keeps converting after the set 5 seconds.

I’m using Aspose.Cells 7.3.2.0. Is it a problem with my code or with the Aspose.Cells version?

P.S. It is also strange that the attached XLSX file has trouble converting (takes forever), because when converting it first to a XLS file it is ready within seconds.

ExampleFile.zip (38.8 KB)

@dhmderuiter

Thanks for using Aspose APIs.

Please download and use the following sample console application project in VB-NET and it works fine.

If you are unable to download the attachment, you may refer to the following VB-NET code.

Code of the TestInterruptMonitor.vb

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports Aspose.Cells
Imports System.Threading

Public Class TestInterruptMonitor

    'Create InterruptMonitor object
    Private im As InterruptMonitor = New InterruptMonitor

    'This function will create workbook and convert it to Pdf format
    Private Sub CreateWorkbookAndConvertItToPdfFormat()
        'Create a workbook object
        Dim wb As Workbook = New Workbook

        'Assign it InterruptMonitor object
        wb.InterruptMonitor = Me.im

        'Access first worksheet
        Dim ws As Worksheet = wb.Worksheets(0)

        'Access cell J1000000 and add some text inside it.
        Dim cell As Cell = ws.Cells("J1000000")
        cell.PutValue("This is text.")

        'Save the workbook to Pdf format          
        wb.Save("output.pdf")
    End Sub

    'This function will interrupt the conversion process after 10s
    Private Sub WaitForWhileAndThenInterrupt()
        Thread.Sleep((1000 * 10))
        Me.im.Interrupt
    End Sub

    Public Sub Run()
        Dim ts1 As ThreadStart = New ThreadStart(AddressOf Me.CreateWorkbookAndConvertItToPdfFormat)

        Dim t1 As Thread = New Thread(ts1)
        t1.Start

        Dim ts2 As ThreadStart = New ThreadStart(AddressOf Me.WaitForWhileAndThenInterrupt)

        Dim t2 As Thread = New Thread(ts2)
        t2.Start

        t1.Join()
        t2.Join
    End Sub
End Class

A post was split to a new topic: Interrupt the save operation

The issues you have found earlier (filed as WORDSNET-11575) have been fixed in this Aspose.Words for .NET 22.1 update also available on NuGet.