{"record":{"id":"739f26fabbce8805","repo":"dotnet/wpf","slug":"sr-format-sr-defaultvaluemustbefreethreaded-propertyname","errorCode":null,"errorMessage":"SR.Format(SR.DefaultValueMustBeFreeThreaded, propertyName)","messagePattern":"SR\\.Format\\(SR\\.DefaultValueMustBeFreeThreaded, propertyName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":423,"sourceCode":"                if (defaultValue is DispatcherObject dispatcherObject && dispatcherObject.Dispatcher != null)\n                {\n                    // Try to make the DispatcherObject free-threaded if it's an\n                    // ISealable.\n\n\n                    if (dispatcherObject is ISealable valueAsISealable && valueAsISealable.CanSeal)\n                    {\n                        Invariant.Assert(!valueAsISealable.IsSealed,\n                               \"A Sealed ISealable must not have dispatcher affinity\");\n\n                        valueAsISealable.Seal();\n\n                        Invariant.Assert(dispatcherObject.Dispatcher == null,\n                            \"ISealable.Seal() failed after ISealable.CanSeal returned true\");\n                    }\n                    else\n                    {\n                        throw new ArgumentException(SR.Format(SR.DefaultValueMustBeFreeThreaded, propertyName));\n                    }\n                }\n            }\n\n\n            // After checking for correct type, check default value against\n            //  validator (when one is given)\n            if ( validateValueCallback != null &&\n                !validateValueCallback(defaultValue))\n            {\n                throw new ArgumentException(SR.Format(SR.DefaultValueInvalid, propertyName));\n            }\n        }\n\n\n        /// <summary>\n        ///     Parameter validation for OverrideMetadata, includes code to force\n        /// all base classes of \"forType\" to register their metadata so we know","sourceCodeStart":405,"sourceCodeEnd":441,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L405-L441","documentation":"For properties whose type is DispatcherObject-derived, a default value carrying thread affinity (Dispatcher != null) cannot be shared across threads, and if it is neither free-threaded nor ISealable, WPF rejects it. Only free-threaded or sealable defaults are accepted so the metadata can be safely shared across all uses.","triggerScenarios":"Registering a property of type e.g. Brush/Geometry/Freezable-derived with a default instance that has a Dispatcher (created on the UI thread) and is not frozen/sealable in metadata.","commonSituations":"Passing a live UI-created Brush (e.g. new SolidColorBrush picked up from a control) as a default; defaults created on a background thread then used in multi-threaded scenarios.","solutions":["Freeze the default Freezable before putting it in metadata (call Freeze() so it becomes free-threaded).","Use a shared static default created once and frozen.","Choose a plain type without thread affinity (e.g. Color instead of SolidColorBrush) if possible."],"exampleFix":"// before\nvar brush = new SolidColorBrush(Colors.Red);\nnew PropertyMetadata(brush);\n// after\nvar brush = new SolidColorBrush(Colors.Red);\nbrush.Freeze();\nnew PropertyMetadata(brush);","handlingStrategy":"validation","validationCode":"if (defaultValue is DispatcherObject d && d.Dispatcher != null && defaultValue is not ISealable) throw new ArgumentException(\"Default must be free-threaded\");","typeGuard":"static bool IsFreeThreadedDefault(object v) => v is not DispatcherObject d || d.Dispatcher == null || v is Freezable f && f.IsFrozen;","tryCatchPattern":null,"preventionTips":["Freeze() Freezable defaults before storing them in metadata","Create shared static defaults once and freeze them","Prefer affinity-free types (Color, double) for defaults"],"tags":["wpf","dependency-property","thread-affinity"],"backgroundTag":"thread-interrupted","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"}