←Select platform

TimeoutMilliseconds Property

Summary

Timeout in milliseconds, after which time loading is aborted.

Syntax
C#
C++/CLI
Python
public int TimeoutMilliseconds {get; set;} 
public:  
   property Int32 TimeoutMilliseconds 
   { 
      Int32 get() 
      void set(Int32 value) 
   } 
TimeoutMilliseconds # get and set (LoadDocumentOptions) 

Property Value

Timeout in milliseconds, after which time loading is aborted. The default value is 0.

Remarks

The default value is 0, meaning there is no timeout set. The DocumentFactory will try to load slow documents regardless of long it takes.

Complex document file formats such as DOCX and XSLX can require significantly more time to parse the file structure than simpler file formats. The amount of time depends on the source file itself, and very complex document files such as a large XLSX spreadsheet with thousands or millions of rows, can take seconds or even minutes. DocumentFactory.LoadFromFile or DocumentFactory.LoadFromUri will not return until all the file data is parsed.

For such documents, using the TimeoutMilliseconds allows the user to abort long load operations if required. After the allocated timeout has passed, DocumentFactory will abort the load operation and null instead of a valid LEADDocument is returned from LoadFromFile or LoadFromUri.

In other words, aborting a load operation does not throw an exception. Instead, the application should check the returned LEADDocument: if it is null, the user aborted.

Note that TimeoutMilliseconds is an approximate value and only measures the time it took the RasterCodecs object to extract information about the document such as its format, number of pages, and the size of each page with the initial GetInformation call. Times for other operations such as downloading the image data from an external URL are not included in TimeoutMilliseconds.

Internally, the value of TimeoutMilliseconds is set into the RasterCodecs.Options.Timeout.TimeoutMilliseconds property of the RasterCodecs object used to load this document. Therefore, not all types of documents can have their load operation aborted by this property. Refer to CodecsTimeoutOptions for more information.

Some applications may have a requirement to abort all load operations that take more than a certain time, for instance a web server that uses LEADTOOLS to load documents in a web method. This server may require that all operations running within the web method to not take more than 2 seconds. When this occurs, the operation should be aborted, and the work can delegate to a dedicated thread or process outside of the web service worker thread.

The time it takes to load a document is recorded and stored in LEADDocument.LoadDuration.

Example

This example will set a set a timeout of 2 seconds and then tries to load a complex document. The sample may or may not work depending on the document and machine speed.

C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
 
public string TryLoadDocument(Uri documentUri) 
{ 
   // This method returns the document ID in the cache if the document was loaded 
   // successfully, or null if the document took more than 2 seconds to load. 
   string documentId; 
 
   // The cache we are using 
   var cache = new FileCache(); 
   cache.PolicySerializationMode = CacheSerializationMode.Json; 
   cache.DataSerializationMode = CacheSerializationMode.Json; 
   cache.CacheDirectory = @"c:\cache-dir"; 
 
   // Try to load the document and save into the cache 
   var loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.Cache = cache; 
   // Set the timeout to 2 seconds 
   loadDocumentOptions.TimeoutMilliseconds = 2 * 1000; 
   LEADDocument document = DocumentFactory.LoadFromUri(documentUri, loadDocumentOptions); 
   if (document != null) 
   { 
      Console.WriteLine($"Success, it took {document.LoadDuration} ms to load this document"); 
      // We managed to load it, save it into the cache and return the document ID 
      document.AutoSaveToCache = false; 
      document.AutoDeleteFromCache = false; 
      document.SaveToCache(); 
      documentId = document.DocumentId; 
      document.Dispose(); 
   } 
   else 
   { 
      // LoadFromUri did not throw an exception, therefore, documentUri 
      // is a valid image/document format that LEADTOOLS understands but the 
      // operation timed out 
      Console.WriteLine("Timed-out"); 
      documentId = null; 
   } 
 
   return documentId; 
} 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
 
 
public void timeoutMillisecondsExample() throws URISyntaxException { 
   URI documentUri = new URI("http://demo.leadtools.com/images/pdf/leadtools.pdf"); 
   // This method returns the document ID in the cache if the document was loaded 
   // successfully, or null if the document took more than 2 seconds to load. 
   String documentId; 
 
   // The cache we are using 
   FileCache cache = new FileCache(); 
   cache.setPolicySerializationMode(CacheSerializationMode.JSON); 
   cache.setDataSerializationMode(CacheSerializationMode.JSON); 
   cache.setCacheDirectory("c:\\cache-dir"); 
 
   // Try to load the document and save into the cache 
   LoadDocumentOptions loadDocumentOptions = new LoadDocumentOptions(); 
   loadDocumentOptions.setCache(cache); 
   // Set the timeout to 2 seconds 
   loadDocumentOptions.setTimeoutMilliseconds(2 * 1000); 
   LEADDocument document = DocumentFactory.loadFromUri(documentUri, loadDocumentOptions); 
   assertTrue(document != null); 
   if (document != null) { 
      System.out.println("Success, it took " + document.getLoadDuration() + " ms to load this document"); 
      // We managed to load it, save it into the cache and return the document ID 
      document.setAutoSaveToCache(false); 
      document.setAutoDeleteFromCache(false); 
      document.saveToCache(); 
      documentId = document.getDocumentId(); 
      document.dispose(); 
   } 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Document Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.