public async Task <OcrResult> QueueForeground(IOcrEngine ocrEngine, ScannedImage.Snapshot snapshot, string tempImageFilePath, OcrParams ocrParams, CancellationToken cancelToken) { OcrRequest req; lock (this) { ocrEngine = ocrEngine ?? ocrManager.ActiveEngine ?? throw new ArgumentException("No OCR engine available"); ocrParams = ocrParams ?? ocrManager.DefaultParams; var reqParams = new OcrRequestParams(snapshot, ocrEngine, ocrParams); req = requestCache.GetOrSet(reqParams, () => new OcrRequest(reqParams)); // Fast path for cached results if (req.Result != null) { SafeDelete(tempImageFilePath); return(req.Result); } // Manage ownership of the provided temp file if (req.TempImageFilePath == null) { req.TempImageFilePath = tempImageFilePath; } else { SafeDelete(tempImageFilePath); } // Increment the reference count req.ForegroundCount += 1; queueWaitHandle.Release(); } // If no worker threads are running, start them EnsureWorkerThreads(); // Wait for completion or cancellation await Task.Factory.StartNew(() => { try { WaitHandle.WaitAny(new[] { req.WaitHandle, cancelToken.WaitHandle }); } catch (Exception e) { Log.ErrorException("Error in OcrRequestQueue.QueueForeground response task", e); } }, TaskCreationOptions.LongRunning); lock (this) { // Decrement the reference count req.ForegroundCount -= 1; // If all requestors have cancelled and there's no result to cache, delete the request DestroyRequest(req); } // If no requests are pending, stop the worker threads EnsureWorkerThreads(); // May return null if cancelled return(req.Result); }
public bool HasCachedResult(IOcrEngine ocrEngine, ScannedImage.Snapshot snapshot, OcrParams ocrParams) { ocrEngine = ocrEngine ?? ocrManager.ActiveEngine ?? throw new ArgumentException("No OCR engine available"); ocrParams = ocrParams ?? ocrManager.DefaultParams; var reqParams = new OcrRequestParams(snapshot, ocrEngine, ocrParams); lock (this) { return(requestCache.ContainsKey(reqParams) && requestCache[reqParams].Result != null); } }
public void QueueBackground(ScannedImage.Snapshot snapshot, string tempImageFilePath, OcrParams ocrParams) { OcrRequest req; CancellationTokenSource cts = new CancellationTokenSource(); lock (this) { var ocrEngine = ocrManager.ActiveEngine; if (ocrEngine == null) { return; } ocrParams = ocrParams ?? ocrManager.DefaultParams; var reqParams = new OcrRequestParams(snapshot, ocrEngine, ocrParams); req = requestCache.GetOrSet(reqParams, () => new OcrRequest(reqParams)); // Fast path for cached results if (req.Result != null) { return; } // Manage ownership of the provided temp file if (req.TempImageFilePath == null) { req.TempImageFilePath = tempImageFilePath; } else { SafeDelete(tempImageFilePath); } // Increment the reference count req.BackgroundCount += 1; snapshot.Source.ThumbnailInvalidated += (sender, args) => cts.Cancel(); snapshot.Source.FullyDisposed += (sender, args) => cts.Cancel(); queueWaitHandle.Release(); } // If no worker threads are running, start them EnsureWorkerThreads(); var op = StartingOne(); Task.Factory.StartNew(() => { try { WaitHandle.WaitAny(new[] { req.WaitHandle, cts.Token.WaitHandle, op.CancelToken.WaitHandle }); lock (this) { // Decrement the reference count req.BackgroundCount -= 1; // If all requestors have cancelled and there's no result to cache, delete the request DestroyRequest(req); } FinishedOne(); // If no requests are pending, stop the worker threads EnsureWorkerThreads(); } catch (Exception e) { Log.ErrorException("Error in OcrRequestQueue.QueueBackground response task", e); } }, TaskCreationOptions.LongRunning); }
public OcrRequest(OcrRequestParams reqParams) { Params = reqParams; }