dotnet/wpf · error · InvalidOperationException

Gesture recognition failed.

Error message

Gesture recognition failed.

What it means

After the native gesture-recognition call (Recognize on the ink context), the returned HRESULT is checked in a finally block. Any native failure — including those from malformed ink data or a broken recognition engine — is reported as a bare InvalidOperationException with SR.UnspecifiedGestureException, hiding COM details.

Solutions

  1. Catch InvalidOperationException around Recognize and treat recognition as failed (return no results)
  2. Validate strokes are non-empty, valid, and fully rendered before recognition
  3. Re-create the GestureRecognizer / native context if failures repeat
  4. Ensure the Tablet PC ink platform is correctly installed on the target OS

Example fix

// before
var results = recognizer.Recognize(strokes);
// after
GestureRecognitionResult[] results;
try { results = recognizer.Recognize(strokes); }
catch (InvalidOperationException) { results = Array.Empty<GestureRecognitionResult>(); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (strokes == null || strokes.Count == 0) return Array.Empty<GestureRecognitionResult>();

Type guard

static bool Recognizable(StrokeCollection s) => s != null && s.Count >= 1 && s.Count <= 2;

Try / catch

try { return recognizer.Recognize(strokes); }
catch (InvalidOperationException) { return Array.Empty<GestureRecognitionResult>(); }

Prevention

When it happens

Trigger: Calling GestureRecognizer.Recognize when the native recognition API returns a failed HRESULT: corrupt or unsupported stroke data, invalidated native context, or ink platform failure.

Common situations: Recognizing strokes that originated from deserialized/foreign ink; engine or Tablet PC service failing at runtime; races where the recognizer context is torn down during recognition.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/8f86dac333a775f7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/GestureRecognizer/NativeRecognizer.cs:178

                if (HRESULT.Succeeded(hr))
                {
                    if ( s_GetAlternateListExists )
                    {
                        recResults = InvokeGetAlternateList();
                    }
                    else
                    {
                        recResults = InvokeGetLatticePtr();
                    }
                }
            }
            finally
            {
                // Check if we should report any error.
                if ( HRESULT.Failed(hr) )
                {
                    //don't throw a com exception here, we don't need to pass out any details
                    throw new InvalidOperationException(SR.UnspecifiedGestureException);
                }
            }

            return recResults;
        }


        internal static ApplicationGesture[] GetApplicationGestureArrayAndVerify(IEnumerable<ApplicationGesture> applicationGestures)
        {
            ArgumentNullException.ThrowIfNull(applicationGestures);

            uint count = 0;
            //we need to make a disconnected copy
            ICollection<ApplicationGesture> collection = applicationGestures as ICollection<ApplicationGesture>;
            if (collection != null)
            {
                count = (uint)collection.Count;
            }

View on GitHub (pinned to 81131a70a4)