dotnet/wpf · error · InvalidOperationException
SR.PenService_WindowNotRegistered
Error message
SR.PenService_WindowNotRegistered
What it means
WispLogic (WispLogic.cs:3226) throws InvalidOperationException(SR.PenService_WindowNotRegistered) when a lookup for the PenContexts of a given PresentationSource returns null — i.e. the window was never registered with the pen service (or was already unregistered). The stylus stack cannot route tablet input for an unregistered window.
Solutions
- Register the window's PresentationSource with the stylus service before any PenContexts-dependent call.
- Check the window/source is still open and registered before issuing stylus commands.
- Ensure unregistration on close does not run earlier than subsequent stylus calls in your shutdown sequence.
Example fix
// before
void OnClose(object s, EventArgs e) { DisableStylus(window); window.Close(); Unregister(window); }
// after
void OnClose(object s, EventArgs e) { window.Close(); Unregister(window); } // unregister last Defensive patterns
Strategy: validation
Validate before calling
if (window == null || !window.IsLoaded) return; // window must be open and registered before stylus calls
Try / catch
try { EnableStylusForWindow(window); } catch (InvalidOperationException ex) when (ex.Message.Contains("not registered")) { /* register the source first, then retry once */ } Prevention
- Register the window's PresentationSource before any stylus/PenContexts API
- Perform stylus teardown after window close, never before dependent calls
- Check window lifetime (IsLoaded) before stylus operations
When it happens
Trigger: Calling WispLogic APIs that resolve PenContexts for an input source (e.g. enabling/disabling stylus for a window, toggling RealTimeStylus paths) before RegisterStylusPointProcessor/source registration, or after the source was unregistered on close.
Common situations: Calling stylus enable/disable APIs in a window constructor before the HwndSource exists; operating on a closed window; mismatched ordering between plugin registration and use across app lifetime events.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- SR.PenService_WindowAlreadyRegistered
- captureMode
- captureMode
- InvalidOperationException
- SR.DuplicateEventName
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/016f7789f7973b1a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/WispLogic.cs:3226
if (__penContextsMap.TryGetValue(hwndSource, out penContexts))
{
__penContextsMap.Remove(hwndSource);
//
// If the application dispatcher is being shut down, we should destroy our pen thread as well.
penContexts.Disable(shutdownWorkThread);
// Make sure we remember the last location of this window for mapping stylus input later.
if (UnsafeNativeMethods.IsWindow(new HandleRef(hwndSource, hwndSource.Handle)))
{
penContexts.DestroyedLocation = PointUtil.ClientToScreen(new Point(0, 0), hwndSource);
}
}
// If we failed to find penContexts for this window above then throw an error now.
if (penContexts == null)
{
throw new InvalidOperationException(SR.PenService_WindowNotRegistered);
}
}
}
/////////////////////////////////////////////////////////////////////
internal PenContexts GetPenContextsFromHwnd(PresentationSource presentationSource)
{
// Only safe to call from UI thread since only it will change Map.
Debug.Assert(Dispatcher.CheckAccess());
PenContexts penContexts = null;
if (presentationSource != null)
{
__penContextsMap.TryGetValue(presentationSource, out penContexts);
}View on GitHub (pinned to 81131a70a4)