How can I add timeouts to PDFNet APIs on .Net?

Q:

I want to convert PDF to XPS (public static void ToXps(PDFDoc in_pdfdoc, string in_filename)) , but I want to limit it for 60 seconds.

Is there any way to limit it by using some PDFtron method or something else that you suggest?

A:

The PDFNet SDK does not provide a mechanism to time-out calls to ToXps.

However, since it appears you are using the .Net API, you could use .Net’s built-in mechanisms for timing out functions, such as:

http://stackoverflow.com/questions/1410602/how-do-set-a-timeout-for-a-method
http://www.developerfusion.com/code/4497/i-need-a-timeout/

Because ToXod is not designed to be run with a timeout, a timeout being enforced on the method is considered to be an exceptional state for it. Thus, ToXod may (correctly) throw exceptions when timed out.

To work around this you can simply catch any exceptions it might throw. For example, you could do something like:

class Program
{
private static bool is_aborted = false;

public static void Main(string[] args)
{
pdftron.PDFNet.Initialize();
string strInFile = args[0];
string strOutFile = args[1];
string strAfterFile = “after.xps”;

using (FileStream fs = new FileStream(strInFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (PDFDoc pdfDoc = new PDFDoc(fs))
{
Thread t = new Thread(() => PdfToXpsLimit(pdfDoc, strAfterFile));
t.Start();

if (!t.Join(TimeSpan.FromSeconds(1)))
{
t.Abort();

is_aborted = true;

try
{
throw new Exception(“Timeout while trying convert.”);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
}

public static void PdfToXpsLimit(PDFDoc pdfnetDoc, string strFilePath)
{

try
{
pdftron.PDF.Convert.ToXps(pdfnetDoc, strFilePath);
}
catch (PDFNetException e)
{
if (!is_aborted) throw;
}
}
}