Using multiple threads allows your application to fully utilize the available computational power. Typically the optimum performance is achieved when number of concurrent threads using ClearImage is limited to twice the number of CPU cores (or hyper-threaded cores, if applicable)
This example shows processing multiple files using multiple threads (.NET 4.0 and higher).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void ProcessFilesThreaded (string[] fileNames) { try { ParallelOptions parallelOptions = new ParallelOptions(); parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount * 2; Parallel.ForEach(fileNames, parallelOptions, fileName => {ProcessFile(fileName);} ); // Parallel } catch (Exception ex) { ProcessException(ex); } } |
This example demonstrates the use of the FileSystemWatcher to monitor the input folder and process image files.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// NOTE: This method is called on a separate thread void onCreateFile(object source, FileSystemEventArgs e) {ProcessFile(e.FullPath);} List<FileSystemWatcher> watchers = new List<FileSystemWatcher>(); void WatchFolder(string path) { if (!Directory.Exists(path)) return; if (watchers.Count > 0 && watchers[0].Path == path) return; // already watching // Add Watchers foreach (string ext in new string[] { "pdf", "tif", "bmp", "jpg" }) { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = path; watcher.Filter = "*." + ext; watcher.Created += new FileSystemEventHandler(onCreateFile); watcher.EnableRaisingEvents = true; watchers.Add(watcher); } } |
This example shows processing multiple files using multiple threads (.NET 4.0 and higher).
|
1 2 3 4 5 6 7 8 9 10 11 |
Private Sub ProcessFilesThreaded(fileNames As String()) Try Dim parallelOptions As New ParallelOptions() parallelOptions.MaxDegreeOfParallelism = Environment.ProcessorCount * 2 Parallel.ForEach(fileNames, parallelOptions, Sub(fileName) ProcessFile(fileName) End Sub) Catch ex As Exception ProcessException(ex) End Try End Sub |
This example demonstrates the use of the FileSystemWatcher to monitor the input folder and process image files.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
' NOTE: This method is called on a separate thread Private Sub onCreateFile(source As Object, e As FileSystemEventArgs) ProcessFile(e.FullPath) End Sub Private watchers As New List(Of FileSystemWatcher)() Private Sub WatchFolder(path As String) If Not Directory.Exists(path) Then Return End If If watchers.Count > 0 AndAlso watchers(0).Path = path Then Return End If ' already watching ' Add Watchers For Each ext As String In New String() {"pdf", "tif", "bmp", "jpg"} Dim watcher As New FileSystemWatcher() watcher.Path = path watcher.Filter = "*." & ext watcher.Created += New FileSystemEventHandler(AddressOf onCreateFile) watcher.EnableRaisingEvents = True watchers.Add(watcher) Next End Sub |
This example demonstrates the processing of image files from a single folder using the anonymous thread method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
static void ReadBarcode1DfromFile(ICiBarcodePro reader, String fileName) { try { int page = 1; while (true) { // Open Image file reader.getImage().Open(fileName, page); // Read barcodes reader.Find(0); // Process results for (int i = 1; i <= reader.getBarcodes().getCount(); i++) { ICiBarcode Bc = reader.getBarcodes().getItem(i); System.out.println(Bc.getText() + " file: " + fileName + " page: " + page + " thread: " + Thread.currentThread().getName()); } page++; if (page > reader.getImage().getPageCount()) break; } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed objects System.gc(); } } protected static void ReadBarcode1DfromFolderWithThreads(String dirName) { // get list of files to process File folder = new File(dirName); File[] listOfFiles = folder.listFiles(); final Vector<String> files = new Vector<String>(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { files.add(listOfFiles[i].getAbsolutePath()); } } int maxThreads = Runtime.getRuntime().availableProcessors(); Thread[] threads = new Thread[maxThreads]; for (int i = 0; i < threads.length; i++) { threads[i] = new Thread(new Runnable() { public void run() { // Anonymous method CiServer objCi; ICiServer Ci; ICiBarcodePro reader; try { // Create and configure barcode reader objCi = new CiServer(); Ci = objCi.getICiServer(); reader = Ci.CreateBarcodePro(); reader.setType(new FBarcodeType( FBarcodeType.cibfCode39, FBarcodeType.cibfCode128)); } catch (Exception ex) { dump_error(ex); return; } while (true) { // Get next file to process String file; synchronized (lockObject) { if (files.size() == 0) break; file = files.remove(0); } try { ReadBarcode1DfromFile(reader, file); } catch (Exception ex) { dump_error(ex); } } } }); threads[i].start(); } if (true) { // Wait for completion (optional) for (int i = 0; i < threads.length; i++) { try { threads[i].join(); threads[i] = null; } catch (InterruptedException ex) { dump_error(ex); } } } } |
