/// <summary> /// Start recognizing a character in a BG thread after strokes have changed. /// </summary> private void startNewCharRecog(IEnumerable <WritingPad.Stroke> strokes) { // If there are other matchers running, stop them now lock (runningMatchers) { foreach (StrokesMatcher sm in runningMatchers) { sm.Stop(); } } // Convert stroke data to HanziLookup's format WrittenCharacter wc = new WrittenCharacter(); foreach (WritingPad.Stroke stroke in strokes) { WrittenStroke ws = new WrittenStroke(); foreach (PointF p in stroke.Points) { WrittenPoint wp = new WrittenPoint((int)(p.X), (int)(p.Y)); ws.AddPoint(wp, ref wc.LeftX, ref wc.RightX, ref wc.TopY, ref wc.BottomY); } wc.AddStroke(ws); } if (wc.StrokeList.Count == 0) { // Don't bother doing anything if nothing has been input yet (number of strokes == 0). ctrlCharPicker.SetItems(null); return; } ThreadPool.QueueUserWorkItem(recognize, wc); }
/// <summary> /// Character recognition worker function (run in worker thread). /// </summary> private void recognize(object ctxt) { bool error = false; char[] matches = null; StrokesMatcher matcher = null; try { WrittenCharacter wc = ctxt as WrittenCharacter; CharacterDescriptor id = wc.BuildCharacterDescriptor(); strokesData.Reset(); matcher = new StrokesMatcher(id, searchScript != SearchScript.Simplified, searchScript != SearchScript.Traditional, Magic.HanziLookupLooseness, Magic.HanziLookupNumResults, strokesData); int matcherCount; lock (runningMatchers) { runningMatchers.Add(matcher); matcherCount = runningMatchers.Count; } while (matcher.IsRunning && matcherCount > 1) { Thread.Sleep(50); lock (runningMatchers) { matcherCount = runningMatchers.Count; } } if (!matcher.IsRunning) { lock (runningMatchers) { runningMatchers.Remove(matcher); matcher = null; } return; } matches = matcher.DoMatching(); if (matches == null) { return; } } catch (DiagnosticException dex) { // Errors not handled locally are only for diagnostics // We actually handle every real-life exception locally here. if (!dex.HandleLocally) { throw; } error = true; AppErrorLogger.Instance.LogException(dex, false); } catch (Exception ex) { error = true; AppErrorLogger.Instance.LogException(ex, false); } finally { if (matcher != null) { lock (runningMatchers) { runningMatchers.Remove(matcher); } } } InvokeOnForm((MethodInvoker) delegate { if (error) { ctrlCharPicker.SetError(); } else { ctrlCharPicker.SetItems(matches); } }); }
/// <summary> /// Start recognizing a character in a BG thread after strokes have changed. /// </summary> private void startNewCharRecog(IEnumerable<WritingPad.Stroke> strokes) { // If there are other matchers running, stop them now lock (runningMatchers) { foreach (StrokesMatcher sm in runningMatchers) sm.Stop(); } // Convert stroke data to HanziLookup's format WrittenCharacter wc = new WrittenCharacter(); foreach (WritingPad.Stroke stroke in strokes) { WrittenStroke ws = new WrittenStroke(); foreach (PointF p in stroke.Points) { WrittenPoint wp = new WrittenPoint((int)(p.X), (int)(p.Y)); ws.AddPoint(wp, ref wc.LeftX, ref wc.RightX, ref wc.TopY, ref wc.BottomY); } wc.AddStroke(ws); } if (wc.StrokeList.Count == 0) { // Don't bother doing anything if nothing has been input yet (number of strokes == 0). ctrlCharPicker.SetItems(null); return; } ThreadPool.QueueUserWorkItem(recognize, wc); }