dotnet/wpf · error · InvalidOperationException

SR.OperationCannotBePerformed

Error message

SR.OperationCannotBePerformed

What it means

WindowsScrollBar.SetScrollValue calls Misc.GetScrollInfo to read the current SCROLLINFO of the target bar; if the Win32 GetScrollInfo call fails it throws InvalidOperationException(SR.OperationCannotBePerformed). The RangeValue.SetValue operation cannot proceed because the scrollbar's state is unreadable.

Solutions

  1. Re-find the AutomationElement (fresh handle) and retry SetValue; cached/stale hwnd is the usual cause.
  2. Verify the element truly supports RangeValuePattern before calling SetValue.
  3. Catch InvalidOperationException and treat as a transient automation failure with retry.

Example fix

// before
scrollBarElement.GetCurrentPattern(RangeValuePattern.Pattern).SetValue(50);
// after
var el = treeElement.FindFirst(TreeScope.Children, cond); // re-find fresh element
((RangeValuePattern)el.GetCurrentPattern(RangeValuePattern.Pattern)).SetValue(50);
Defensive patterns

Strategy: retry

Validate before calling

if (element.Current.IsOffscreen || !element.Current.IsEnabled) reFind();
// ensure RangeValue support
bool supported = element.TryGetCurrentPattern(RangeValuePattern.Pattern, out var p);

Try / catch

try { rv.SetValue(val); }
catch (InvalidOperationException) { element = ReFindElement(); ((RangeValuePattern)element.GetCurrentPattern(RangeValuePattern.Pattern)).SetValue(val); }

Prevention

When it happens

Trigger: RangeValuePattern.SetValue on a scrollbar/trackbar-backed element where GetScrollInfo(_hwnd, _sbFlag, ref si) returns false, at WindowsScrollBar.cs:629.

Common situations: Targeting a scrollbar handle that has been destroyed or recreated between finding the element and setting the value; targeting a control that only pretends to expose the RangeValue pattern; handle invalidation after window layout changes.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsScrollBar.cs:629

        }

        private void SetScrollValue (int val)
        {
            // Check if the window is disabled
            if (!SafeNativeMethods.IsWindowEnabled (_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            NativeMethods.ScrollInfo si = new NativeMethods.ScrollInfo
            {
                fMask = NativeMethods.SIF_ALL
            };
            si.cbSize = Marshal.SizeOf (si.GetType ());

            if (!Misc.GetScrollInfo(_hwnd, _sbFlag, ref si))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

            // No move, exit
            if (val == si.nPos)
            {
                return;
            }

            // NOTE:
            // Proportional scrollbars have a few key values: min, max, page, and current.
            // 
            // Min and max represent the endpoints of the scrollbar; page is the side of the thumb,
            // and current is the position of the leading edge of the thumb (top for a vert scrollbar).
            // 
            // Because of this arrangment, current can't be any value between min and max, it's actually
            // confied to min...(max-page)+1. That +1 is a quirk of the scrollbar's internal logic.
            // 
            // For example, in an edit in notepad, these might be:

View on GitHub (pinned to 81131a70a4)