Options for the global filter operation timeout used by this RasterCodecs.
public class CodecsTimeoutOptions
Public Class CodecsTimeoutOptions
ref class CodecsTimeoutOptions
CodecsTimeoutOptions contains options for the global filter operation timeout used by this RasterCodecs. This class contains the following members:
|TimeoutMilliseconds||Timeout value in milliseconds. The default value is 0, meaning there is no timeout set.|
The RasterCodecs class contains the LoadImage event that can be used to monitor the progress of loading images using Load or LoadSvg. The LoadImage event is very suitable for raster images since the filter (PNG or TIF, for instance) can parse the image file structure quickly and start to send one or more scanlines to the event quickly. Therefore,
LoadImage will start to occur shortly after
LoadSvg is called. The application can use
LoadImage to show a progress bar to allow the user to monitor and potentially abort the load operation.
For complex document file formats such as DOCX and XSLX, this is not possible. The file filter will require significantly more time to parse the file structure. And the first scanline generated and sent to the
LoadImage event may not occur until after a considerable amount of time has passed since
LoadSvg was called. This amount of time depends on the source file itself, and for very complex document files such as a very large XLSX spreadsheet with thousands or millions of rows, the time can be in seconds or even minutes.
Moreover, the parsing of the file structure usually occurs during GetInformation (whether called directly by the user or internally by
LoadSvg), which does not invoke the LoadImage event at all.
For these kinds of documents, using the
LoadImage event to allow the user to abort long load operations is not feasible: the event will occur at the very end of the operation after all the necessary data has been parsed and a significant amount of time could have been elapsed since the original
LoadSvg was called.
Instead, the application can use the RasterCodecs.FilterHeartbeat event to monitor and abort these very long operations. This event occurs at the very start of
GetInformation and then periodically during this function and
LoadSvg to allow the user to abort the current operation if desired.
GetInformationwill return a CodecsImageInfo with CodecsImageInfo.Format set to RasterImageFormat.Unknown and all the rest of the members set to their default values.
LoadSvgwill return null instead of a valid RasterImage or ISvgDocument object.
In other words, aborting a filter operation does not throw an exception. Instead, the application should check the returned object as described above to detect when the user aborted.
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 and return images in a web method). This server may require that no operation running within the web method can take more than 2 seconds. When this occurs, the operation should be aborted, and the work can be delegated to a dedicated thread or process outside of the web service worker thread.
[FilterHeartbeat] can be used to install an event that tracks the time and aborts all the operations taking more than the allocated time. Or, the RasterCodecs.Options.Timeout property can be used to set a global timeout interval for all get information and load operations in a
RasterCodecs objects. This class uses [FilterHeartbeat] internally to install a handler that can take care of aborting long-running operations automatically.
For an example on using a heartbeat handler, refer to RasterCodecs.FilterHeartbeat.
Currently filter heartbeat and timeout is only supported by the following file formats:
// Call this method with a very large and complex XLSX document and a timeout in seconds value
// Returns true on success and false if aborted
// For each page, the image is loaded and passed to the processImage action
public static bool CodecsTimeoutExample(string inputFileName, int timeoutSeconds, Action<RasterImage, int> processImage)
using (var rasterCodecs = new RasterCodecs())
// Set the timeout
rasterCodecs.Options.Timeout.TimeoutMilliseconds = timeoutSeconds * 1000;
// First, get information on the file to get the number of pages
int pageCount = 0;
using (CodecsImageInfo imageInfo = rasterCodecs.GetInformation(inputFileName, true))
// If GetInformationt took more than timeoutSeconds then RasterCodecs aborted the operation
// and GetInformation will return RasterImageFormat.Unknown
format = imageInfo.Format;
if (format != RasterImageFormat.Unknown)
pageCount = imageInfo.TotalPages;
// Did we abort?
if (format == RasterImageFormat.Unknown)
// Now load all the pages
for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++)
RasterImage image = rasterCodecs.Load(inputFileName, pageNumber);
// If Load took more than timeoutSeconds then RasterCodecs aborted the operation
// and Load will return null
// Did we abort?
if (image == null)
// Yes, fail
// Process the image and then delete it
// We successfully loaded and processed all the pages from the file
' Call this method with a very large And complex XLSX document And a timeout in seconds value
' Returns true on success And false if aborted
' For each page, the image Is loaded And passed to the processImage action
Public Shared Function CodecsTimeoutExample(inputFileName As String, timeoutSeconds As Integer, processImage As Action(Of RasterImage, Integer)) As Boolean
Using rasterCodecs As New RasterCodecs()
' Set the timeout
rasterCodecs.Options.Timeout.TimeoutMilliseconds = timeoutSeconds * 1000
' First, get information on the file to get the number of pages
Dim format As RasterImageFormat
Dim pageCount As Integer = 0
Using imageInfo As CodecsImageInfo = rasterCodecs.GetInformation(inputFileName, True)
' If GetInformationt took more than timeoutSeconds then RasterCodecs aborted the operation
' And GetInformation will return RasterImageFormat.Unknown
format = imageInfo.Format
If format <> RasterImageFormat.Unknown Then
pageCount = imageInfo.TotalPages
' Did we abort?
If format = RasterImageFormat.Unknown Then
' Now load all the pages
For pageNumber As Integer = 1 To pageCount
Dim image As RasterImage = rasterCodecs.Load(inputFileName, pageNumber)
' If Load took more than timeoutSeconds then RasterCodecs aborted the operation
' And Load will return null
' Did we abort?
If IsNothing(image) Then
' Yes, fail
' Process the image And then delete it
' We successfully loaded And processed all the pages from the file
Medical Web Viewer .NET
.NET, Java, Android, and iOS/macOS Assemblies
C API/C++ Class Libraries