{"record":{"id":"003fb3b5c69d8781","repo":"dotnet/wpf","slug":"sr-freezable-cantfreeze","errorCode":null,"errorMessage":"SR.Freezable_CantFreeze","messagePattern":"SR\\.Freezable_CantFreeze","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs","lineNumber":172,"sourceCode":"            get\n            {\n                return IsFrozenInternal || FreezeCore(isChecking: true);\n            }\n        }\n\n        /// <summary>\n        /// Does an in-place modification to make the object frozen. It is legal to\n        /// call this on values that are already frozen.\n        /// </summary>\n        /// <exception cref=\"System.InvalidOperationException\">This exception\n        /// will be thrown if this Freezable can't be frozen. Use\n        /// the CanFreeze property to detect this in advance.</exception>\n        public void Freeze()\n        {\n            // Check up front that the operation will succeed before we begin.\n            if (!CanFreeze)\n            {\n                throw new InvalidOperationException(SR.Freezable_CantFreeze);\n            }\n\n            Freeze(isChecking: false);\n        }\n\n        #endregion\n\n        #region Public Properties\n\n        //------------------------------------------------------\n        //\n        //  Public Properties\n        //\n        //------------------------------------------------------\n\n        /// <summary>\n        /// Returns whether or not the Freezable is modifiable.  Attempts\n        /// to set properties on an IsFrozen value result","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Freezable.cs#L154-L190","documentation":"Freezable.Freeze() makes the object immutable, but only if every part of the object graph is freezable (CanFreeze is true — no databound or mutable sub-objects remain). The public Freeze() first checks CanFreeze and throws InvalidOperationException(SR.Freezable_CantFreeze) when the check fails, guaranteeing the freeze operation will succeed before mutating state.","triggerScenarios":"Calling Freeze() (or GetAsFrozen) on a Freezable whose CanFreeze is false — e.g. a Brush or Geometry containing non-frozen sub-objects, an animation with unresolved bindings, or a Freezable still registered to change handlers.","commonSituations":"Freezing a SolidColorBrush whose Color is animated, a Pen referencing a non-freezable brush, or any resource defined in XAML with DynamicResource/Binding inside; attempting to freeze resources for cross-thread sharing in performance work.","solutions":["Check CanFreeze before calling Freeze and skip/clone the object when it returns false","Remove or replace non-freezable members (Bindings, DynamicResource references, animated values) with fixed values","Use GetAsFrozen/GetCurrentValueAsFrozen appropriately and handle the failure path","Clone the Freezable and simplify it until CanFreeze returns true"],"exampleFix":"// before\nbrush.Freeze(); // throws if animated\n// after\nif (brush.CanFreeze) brush.Freeze();\nelse { var frozen = brush.GetCurrentValueAsFrozen() as SolidColorBrush; }","handlingStrategy":"validation","validationCode":"if (freezable.CanFreeze) freezable.Freeze(); else { /* clone/simplify first */ }","typeGuard":"static bool CanSafelyFreeze(System.Windows.Freezable f) => f.CanFreeze && !f.IsFrozen;","tryCatchPattern":"try { obj.Freeze(); }\ncatch (InvalidOperationException ex) { log.Warn($\"{obj} cannot be frozen: {ex.Message}\"); }","preventionTips":["Always check CanFreeze before Freeze/GetAsFrozen","Avoid Bindings/DynamicResource inside objects you intend to freeze","Replace animated values with fixed values before freezing for cross-thread sharing"],"tags":["wpf","freezable","threading","freeze"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}