←Select platform

RunJobAsync Method

Summary
Runs a job asynchronously
Syntax
C#
VB
Objective-C
C++
Java
public void RunJobAsync( 
   IOcrAutoRecognizeJob job 
) 
Sub RunJobAsync( _ 
   ByVal job As IOcrAutoRecognizeJob _ 
)  
- (BOOL)runJobAsync:(LTOcrAutoRecognizeJob *)job error:(NSError **)error 
public void runJobAsync(OcrAutoRecognizeJob job) 
void RunJobAsync(  
   IOcrAutoRecognizeJob^ job 
)  

Parameters

job
The IOcrAutoRecognizeJob to run this parameter cannot be null (Nothing in VB). Use CreateJob to create a job.

Remarks

This method will create an internal worker thread and return control immediately to the called. When the job is completed, the IOcrAutoRecognizeJob.Errors member of job will contain any errors that might have occurred during the recognition process. To get notification when the job is completed (whether successfully or with aborted due to errors or through user action), subscribe to the JobCompleted events.

To run a job synchronously, use RunJob.

To use this method, initialize a new OcrAutoRecognizeJobData object with the job's parameters (input image file name, pages, output document format, output document name, optional zones file name, etc.), then use CreateJob to create the IOcrAutoRecognizeJob object passed as job to this method. Finally, call RunJobAsync passing the IOcrAutoRecognizeJob object.

This method will perform the following operations:

  1. The JobStarted event is triggered.

  2. Creates one or more IOcrDocument object to store the pages into. The number of OCR documents created is dependent on MaximumThreadsPerJob. If this value is 0 (maximum CPUs/cores) or is greater than 1 and multiple threads is supported by this engine, then more than one document might be created to participate in the recognition process. The document will be created as disk-based.

  3. Loops through the pages specified in OcrAutoRecognizeJobData.FirstPageNumber and in OcrAutoRecognizeJobData.LastPageNumber in OcrAutoRecognizeJobData.ImageFileName or OcrAutoRecognizeJobData.ImageStream and for each page:

  4. When all pages are processed they are saved to result file name specified in OcrAutoRecognizeJobData.DocumentFileName using the format specified OcrAutoRecognizeJobData.Format If LTD was used, the temporary file is converted to the final document using DocumentWriter.Convert and optionally DocumentWriter.AppendLtd.

  5. All OCR documents and temporary files are deleted.

  6. The JobCompleted event is triggered.

  7. You can use the JobProgress event to show the operation progress or to abort it if threading is not used. For more information and an example, refer to OcrProgressCallback.

  8. You can use the JobOperation event to get information regarding the current operation being performed. For more information and an example, refer to JobOperation.

The IOcrAutoRecognizeManager interface also has the following options to use with this method:

Option Description
MaximumPagesBeforeLtd

Add support for converting a document with unlimited number of pages. An OCR recognition operation on a document that contains a large amount of pages (10 and more) might result in an out of memory error.

All of the LEADTOOLS OCR engines supports saving the intermediate recognition results to a temporary LTD file (DocumentFormat.LTD). The result of subsequent pages will be appended to this temporary file. When all the pages of the document have been recognized, the engine will convert the temporary LTD file to the desired output format.

The MaximumPagesBeforeLtd property defines the maximum number of pages processed as a whole. For example, if the original document has 20 pages and the value of this property is 8, the engine will recognize the first 8 pages and saves the result to a temporary file, recognizes the second 8 pages and append the results, and finally, recognize the last 4 pages and convert the temporary document to the final format.

PreprocessPageCommands

Holds an array of OcrAutoPreprocessPageCommand items to control what auto-preprocess operation to perform on each page document prior to recognition.

MaximumThreadsPerJob

Maximum number of threads to use per job. You can instruct IOcrAutoRecognizeManager to use all available machine CPUs/cores when recognizing a document. This will greatly reduce the time required to finish the OCR operation.

JobErrorMode

Ability to resume on none critical errors. For example, if a source document has a page that could not be recognized. The offending page will be added to the final document as a graphics images and recognition will continue to the next page.

JobStarted, JobProgress, JobOperation and JobCompleted events

Events to track when both synchronous and asynchronous jobs has started, being run and completed.

AbortAllJobs

Aborts all running and pending jobs.

EnableTrace

Output debug messages to the standard .NET trace listeners.

Example
C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Document.Writer; 
using Leadtools.Forms.Common; 
using Leadtools.WinForms; 
 
private AutoResetEvent _jobFinishedEvent; 
 
public void RunJobAsyncExample() 
{ 
   string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"); 
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf"); 
 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      Console.WriteLine("Starting up the engine..."); 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      IOcrAutoRecognizeManager ocrAutoRecognizeManager = ocrEngine.AutoRecognizeManager; 
 
      // Create the job 
      OcrAutoRecognizeJobData ocrJobData = new OcrAutoRecognizeJobData(tifFileName, DocumentFormat.Pdf, pdfFileName); 
      ocrJobData.JobName = "MyJob"; 
      IOcrAutoRecognizeJob ocrJob = ocrAutoRecognizeManager.CreateJob(ocrJobData); 
 
      // Create the event 
      _jobFinishedEvent = new AutoResetEvent(false); 
 
      // Run the job in a thread and wait for it to be done 
      // We will use the JobCompleted event to get notified when 
      // the job is finished 
      ocrAutoRecognizeManager.JobCompleted += new EventHandler<OcrAutoRecognizeRunJobEventArgs>(ocrAutoRecognizeManager_JobCompleted); 
      Console.WriteLine("Running the job..."); 
      ocrAutoRecognizeManager.RunJobAsync(ocrJob); 
 
      Console.WriteLine("Waiting for the job to complete..."); 
      _jobFinishedEvent.WaitOne(); 
      Console.WriteLine("Done..."); 
      _jobFinishedEvent.Close(); 
      ocrAutoRecognizeManager.JobCompleted -= new EventHandler<OcrAutoRecognizeRunJobEventArgs>(ocrAutoRecognizeManager_JobCompleted); 
   } 
} 
 
private void ocrAutoRecognizeManager_JobCompleted(object sender, OcrAutoRecognizeRunJobEventArgs e) 
{ 
   // Tell main thread we are done 
   _jobFinishedEvent.Set(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS21\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Ocr 
Imports Leadtools.Document.Writer 
Imports Leadtools.Forms.Common 
Imports Leadtools.WinForms 
 
Private _jobFinishedEvent As AutoResetEvent 
 
Public Sub RunJobAsyncExample() 
   Dim tifFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif") 
   Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf") 
 
   ' Create an instance of the engine 
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD) 
      ' Start the engine using default parameters 
      Console.WriteLine("Starting up the engine...") 
      ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrLEADRuntimeDir) 
 
      Dim ocrAutoRecognizeManager As IOcrAutoRecognizeManager = ocrEngine.AutoRecognizeManager 
 
      ' Create the job 
      Dim ocrJobData As New OcrAutoRecognizeJobData(tifFileName, DocumentFormat.Pdf, pdfFileName) 
      ocrJobData.JobName = "MyJob" 
      Dim ocrJob As IOcrAutoRecognizeJob = ocrAutoRecognizeManager.CreateJob(ocrJobData) 
 
      ' Create the event 
      _jobFinishedEvent = New AutoResetEvent(False) 
 
      ' Run the job in a thread and wait for it to be done 
      ' We will use the JobCompleted event to get notified when 
      ' the job is finished 
      AddHandler ocrAutoRecognizeManager.JobCompleted, AddressOf ocrAutoRecognizeManager_JobCompleted 
      Console.WriteLine("Running the job...") 
      ocrAutoRecognizeManager.RunJobAsync(ocrJob) 
 
      Console.WriteLine("Waiting for the job to complete...") 
      _jobFinishedEvent.WaitOne() 
      Console.WriteLine("Done...") 
      _jobFinishedEvent.Close() 
      RemoveHandler ocrAutoRecognizeManager.JobCompleted, AddressOf ocrAutoRecognizeManager_JobCompleted 
   End Using 
End Sub 
 
Private Sub ocrAutoRecognizeManager_JobCompleted(sender As Object, e As OcrAutoRecognizeRunJobEventArgs) 
   ' Tell main thread we are done 
   _jobFinishedEvent.Set() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\LEADTOOLS21\Resources\Images" 
   Public Const OcrLEADRuntimeDir As String = "C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime" 
End Class 
Requirements

Target Platforms

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

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