Working with threads
ClearImage 8 Help
Working with threads
Send Feedback

Glossary Item Box

ClearImageNet supports multi-threaded execution.  A single ClearImage engine is instantiated for each thread that uses ClearImage API.   Once the thread is terminated, the engine becomes available for another thread.

The number of threads that invoke ClearImage at any time should be equal to or less than the number of cores available to the application.  

Read barcodes from all files in a directory using threads

 

Reading barcodes with threads Copy Code
static object _lockObject = new object();
string[] filesToScan;
long filesScanned = 0;
 
private void ReadFilesOnThread()
  {
  BarcodeReader reader = new BarcodeReader();
  reader.Code39 = true;
  reader.BarcodeFoundEvent += new BarcodeReader.BarcodeFoundEventHandler(_OnBarcodeFoundThread);
    // Read
  while (true)
    {
    string fileName;
      // Obtain the next file name
    lock (_lockObject)
      {
      if (filesScanned >= filesToScan.Length)
        break;
      fileName = filesToScan[filesScanned];
      filesScanned++;
      }
     // Read images from file
    try
      {
      Barcode[] barcodes = reader.Read(fileName); // Read all pages
      foreach (Barcode bc in barcodes) 
        {Console.WriteLine(bc.Text); } 
      }
    catch (Exception ex)
      {
      Console.WriteLine("Error: " + ex.Message); 
      }
    }
  }
 
public void ReadBarcodesWithThreads(string directoryName)
  {  // Collect list of files to read
  filesToScan = Directory.GetFiles(directoryName, "*.*",
  SearchOption.TopDirectoryOnly);
  filesScanned = 0;
     // Start 2 threads
  Thread workerThread1 = new Thread(new ThreadStart(ReadFilesOnThread));
  workerThread1.Start();
  Thread workerThread2 = new Thread(new ThreadStart(ReadFilesOnThread));
  workerThread2.Start();
    // wait for both threads to exit
  while (workerThread1.IsAlive || workerThread2.IsAlive)
    {
    System.Windows.Forms.Application.DoEvents();
    }
  }

 

© 2007-2013. Inlite Research, Inc. All Rights Reserved.