dotnet/wpf · error · InvalidOperationException

Operation cannot be performed.

Error message

Operation cannot be performed.

What it means

After the enabled check, WindowsFormsLinkLabel.Invoke verifies the control is visible with IsWindowVisible; a hidden LinkLabel cannot be clicked, so it throws InvalidOperationException with SR.OperationCannotBePerformed ('Operation cannot be performed').

Solutions

  1. Make the control visible before invoking: show the form and set linkLabel.Visible = true.
  2. Check element.Current.IsOffscreen == false before calling Invoke.
  3. Catch InvalidOperationException and either show the window first or use an alternate non-UI path (call the underlying action directly).
  4. Activate/restore the parent window if it is minimized.

Example fix

// before
invokePattern.Invoke();
// after
if (!element.Current.IsOffscreen)
{
    invokePattern.Invoke();
}
else
{
    form.Show(); // or bring to correct tab
    invokePattern.Invoke();
}
Defensive patterns

Strategy: validation

Validate before calling

bool visible = !element.Current.IsOffscreen && element.Current.IsControlElement;
if (!visible) { /* show form / activate tab before Invoke */ }

Try / catch

try { invokePattern.Invoke(); }
catch (InvalidOperationException) { BringTargetIntoView(element); invokePattern.Invoke(); }

Prevention

When it happens

Trigger: Calling Invoke() on a LinkLabel whose HWND is not visible — control Visible=false, parent form minimized/hidden, or control on a non-active tab page.

Common situations: Automating forms on hidden or minimized windows; clicking links on inactive TabPages; UIA scripts running before the form is shown (pre-Shown).

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsFormsLinkLabel.cs:115

        #region Invoke Pattern

        // Same as clicking on an hyperlink
        void IInvokeProvider.Invoke()
        {
            // Check that button can be clicked.
            //
            // This state could change anytime.
            //

            // Make sure that the control is enabled
            if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
            {
                throw new ElementNotEnabledException();
            }

            if (!SafeNativeMethods.IsWindowVisible(_hwnd))
            {
                throw new InvalidOperationException(SR.OperationCannotBePerformed);
            }

            Misc.SetFocus(_hwnd);

            NativeMethods.Win32Point pt = new NativeMethods.Win32Point();
            if (GetClickablePoint(out pt, false))
            {
                Misc.MouseClick(pt.x, pt.y);
            }
        }

        #endregion Invoke Pattern
   }
}

View on GitHub (pinned to 81131a70a4)