L_SetFilterHeartbeatCallback

#include "l_bitmap.h"

L_LTFIL_API L_INT EXT_CALLBACK L_SetFilterHeartbeatCallback(pCallback, pUserData)

Sets the callback used for tracking and aborting long-running operations.

Parameters

FILTERHEARTBEATCALLBACK pCallback

Your callback function that fires whenever a filter heartbeat occurs.

L_VOID *pUserData

Void pointer that you can use to pass one or more additional parameters that the callback function needs.

To use this feature, assign a value to a variable or create a structure that contains as many fields as you need. Then, in this parameter, pass the address of the variable or structure, casting it to L_VOID *. The callback function, which receives the address in its own pUserData parameter, can cast it to a pointer of the appropriate data type to access your variable or structure.

If the additional parameters are not needed, you can pass NULL in this parameter.

Returns

Value Meaning
SUCCESS The function was successful.
< 1 An error occurred. Refer to Return Codes.

Comments

LEADTOOLS load file functions contains the FILEREADCALLBACK. It is used to monitor the progress of loading images by calling L_LoadFile or L_LoadSvg2. FILEREADCALLBACK is quite suitable for raster images: the filter (PNG or TIF, for instance), can parse the image file structure quickly and start to send one or more scanlines to the callback quickly. Consequently, FILEREADCALLBACK events will start to occur shortly after Load/LoadSvg is called. The application can use FILEREADCALLBACK to show a progress bar to allow the user to monitor and potentially abort the load operation.

FILEREADCALLBACK does not work well with complex document file formats such as DOCX and XSLX. Their complex structure requires significantly more time to parse the file structure. The first scanline generated and sent to FILEREADCALLBACK may not occur until well after Load/LoadSvg was called. The amount of time required for loading depends on the source file itself. For a very complex document file such as a very large XLSX spreadsheet with thousands or millions of rows, the time can be many seconds or even minutes. Moreover, file structure parsing usually occurs by calling L_FileInfo (whether called directly by the user, or internally by Load/LoadSvg), none of which invoke FILEREADCALLBACK at all.

For complex documents, using FILEREADCALLBACK to allow the user to abort long load operations is not feasible: the callback will occur at the very end of the operation after all the necessary data has been parsed. A significant amount of time could have elapsed since the original Load/LoadSvg was called. Instead, the application can use L_SetFilterHeartbeatCallback to monitor and abort very long operations. FILTERHEARTBEATCALLBACK occurs at the beginning of L_FileInfo, and then again periodically during this function and Load/LoadSvg, allowing the user to abort the current operation if desired.

The FILTERHEARTBEATCALLBACK will fire and the application can set the return value to ERROR_USER_ABORT to abort the current operation as follows:

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 require all load operations that take more than a certain time to be aborted (for instance, a web server using 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 delegated to a dedicated thread or process outside of the web service worker thread.

L_SetFilterHeartbeatCallback can be used to install a callback that tracks the time and aborts all the operations taking more than the allocated time. Or, L_SetFilterTimeout can be used to set a global timeout interval for all get information and load operations. This function uses L_SetFilterHeartbeatCallback internally to install a handler that can take care of aborting long-running operations automatically.

Currently filter heartbeat and timeout is only supported for the following file formats:

Required DLLs and Libraries

Platforms

Win32, x64, Linux.

See Also

Functions

Example

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

// 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 
 
class Stopwatch 
{ 
   public: Stopwatch() 
   { 
#if !defined(FOR_UNIX) 
      LARGE_INTEGER li; 
      QueryPerformanceFrequency(&li); 
      _Frequency = (L_DOUBLE)li.QuadPart / 1000.0; 
#else 
      _Frequency = 1; 
#endif 
 
      Reset(); 
   } 
 
   public: L_VOID Reset() 
   { 
#if !defined(FOR_UNIX) 
      LARGE_INTEGER li; 
      QueryPerformanceCounter(&li); 
      _StartTime = (L_DOUBLE)li.QuadPart; 
#else 
      struct timespec li; 
      clock_gettime(CLOCK_REALTIME, &li); 
      _StartTime = ((L_DOUBLE)li.tv_sec * 1000.0) + ((L_DOUBLE)li.tv_nsec / 1.0e6); 
#endif 
   } 
 
   public: L_UINT ElapsedMilliseconds() 
   { 
      L_DOUBLE nowTime; 
 
#if !defined(FOR_UNIX) 
      LARGE_INTEGER li; 
      QueryPerformanceCounter(&li); 
      nowTime = (L_DOUBLE)li.QuadPart; 
#else 
      struct timespec li; 
      clock_gettime(CLOCK_REALTIME, &li); 
      nowTime = ((L_DOUBLE)li.tv_sec * 1000.0) + ((L_DOUBLE)li.tv_nsec / 1.0e6); 
#endif 
 
      L_DOUBLE interval = (nowTime - _StartTime) / _Frequency; 
      return (L_UINT)interval; 
   } 
 
   private: L_DOUBLE _Frequency; 
   private: L_DOUBLE _StartTime; 
}; 
 
class CallbackData 
{ 
   public: Stopwatch Stopwatch; 
   public: L_UINT TimeoutMilliseconds; 
}; 
 
static L_INT EXT_CALLBACK FilterHeartbeatCallback(L_VOID* /*data*/, L_VOID* userData) 
{ 
   // Abort if it has been more than timeoutSeconds since last operation 
   CallbackData* callbackData = reinterpret_cast<CallbackData*>(userData); 
   if(callbackData->Stopwatch.ElapsedMilliseconds() > callbackData->TimeoutMilliseconds) 
   { 
      return ERROR_USER_ABORT; 
   } 
   else 
   { 
      return SUCCESS; 
   } 
} 
 
typedef L_INT(*PROCESS_IMAGE)(BITMAPHANDLE* bitmapHandle, L_INT pageNumber); 
L_INT FilterHeartbeatExample(L_TCHAR* inputFileName, L_UINT timeoutSeconds, PROCESS_IMAGE processImage) 
{ 
   CallbackData callbackData; 
   callbackData.TimeoutMilliseconds = timeoutSeconds * 1000; 
   // This is the start time, we will reset this value 
   // anytime we start an operation 
   callbackData.Stopwatch.Reset(); 
 
   // Set the callback 
   L_INT ret = L_SetFilterHeartbeatCallback(FilterHeartbeatCallback, &callbackData); 
 
   // Reset the start time 
   callbackData.Stopwatch.Reset(); 
 
   // First, get information on the file to get the number of pages 
   int pageCount = 0; 
   FILEINFO fileInfo; 
   memset(&fileInfo, 0, sizeof(FILEINFO)); 
   ret = L_FileInfo(inputFileName, &fileInfo, sizeof(FILEINFO), FILEINFO_TOTALPAGES, NULL); 
 
   // If L_FileInfo took more than timeoutSeconds then it returns ERROR_USER_ABORT 
   if(ret == ERROR_USER_ABORT) 
   { 
      printf("Aborted\n"); 
   } 
 
   // Fail 
   if(ret != SUCCESS) 
   { 
      L_SetFilterHeartbeatCallback(NULL, NULL); 
      return ret; 
   } 
 
   // Now load all the pages 
 
   LOADFILEOPTION loadFileOption; 
   memset(&loadFileOption, 0, sizeof(LOADFILEOPTION)); 
   L_GetDefaultLoadFileOption(&loadFileOption, sizeof(LOADFILEOPTION)); 
 
   for(L_INT pageNumber = 1; pageNumber <= pageCount; pageNumber++) 
   { 
      loadFileOption.PageNumber = pageNumber; 
 
      // Reset the start time 
      callbackData.Stopwatch.Reset(); 
      BITMAPHANDLE bitmapHandle; 
      memset(&bitmapHandle, 0, sizeof(BITMAPHANDLE)); 
      ret = L_LoadBitmap(inputFileName, &bitmapHandle, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, &loadFileOption, NULL); 
 
      // If L_LoadBitmap took more than timeoutSeconds then it returns ERROR_USER_ABORT 
      if(ret == ERROR_USER_ABORT) 
      { 
         printf("Aborted\n"); 
      } 
 
      // Fail 
      if(ret != SUCCESS) 
      { 
         L_SetFilterHeartbeatCallback(NULL, NULL); 
         return ret; 
      } 
 
      // Did we abort? 
      // Process the image and then delete it 
      processImage(&bitmapHandle, pageNumber); 
 
      L_FreeBitmap(&bitmapHandle); 
   } 
 
   L_SetFilterHeartbeatCallback(NULL, NULL); 
 
   // We successfully loaded and processed all the pages from the file 
   return true; 
} 

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

LEADTOOLS Raster Imaging C API Help