{"record":{"id":"bde07146e2715d47","repo":"dotnet/wpf","slug":"sr-requiressta-bitmapeffect","errorCode":null,"errorMessage":"SR.RequiresSTA","messagePattern":"SR\\.RequiresSTA","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/BitmapEffect.cs","lineNumber":28,"sourceCode":"    /// <summary>\n    /// BitmapEffect\n    /// </summary>\n    public abstract partial class BitmapEffect\n    {\n        #region Constructors\n        /// <summary>\n        /// Constructor\n        /// </summary>\n        protected BitmapEffect()\n        {     \n            // STA Requirement\n            //\n            // Avalon doesn't necessarily require STA, but many components do.  Examples\n            // include Cicero, OLE, COM, etc.  So we throw an exception here if the\n            // thread is not STA.\n            if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)\n            {\n                throw new InvalidOperationException(SR.RequiresSTA);\n            }\n        }\n\n        #endregion\n\n        #region Protected Methods\n        /// <summary>\n        /// This method is called before calling GetOutput on an effect.\n        /// It gives a chance for the managed effect to update the properties\n        /// of the unmanaged object.\n        /// </summary>\n        [Obsolete(MS.Internal.Media.VisualTreeUtils.BitmapEffectObsoleteMessage)]\n        protected abstract void UpdateUnmanagedPropertyState(SafeHandle unmanagedEffect);\n\n\n        /// <summary>\n        /// Returns a safe handle to an unmanaged effect clone\n        /// </summary>","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Effects/BitmapEffect.cs#L10-L46","documentation":"A BitmapEffect (or derived effect) was constructed on a thread whose apartment state is not STA. WPF effects rely on STA-only components (Cicero, OLE, COM), so the constructor VerifyRequiresSTA path throws InvalidOperationException with SR.RequiresSTA when Thread.CurrentThread.GetApartmentState() != ApartmentState.STA.","triggerScenarios":"new SomeBitmapEffect() on a background/MTA thread; creating effects inside Task.Run or a non-STA worker; console/service hosts without [STAThread] on Main.","commonSituations":"Unit test runners with MTA threads; headless image processing services creating WPF visuals off-thread; migrating legacy code to async workers; missing [STAThread] attribute after host change.","solutions":["Create and use the effect on the WPF UI (STA) thread, e.g. via Dispatcher.Invoke","Mark the entry point with [STAThread] or start the thread with Thread.SetApartmentState(ApartmentState.STA)","Pre-render into a static image/source on the STA thread, then consume the result on the worker thread","Migrate to the modern Effect (shader) API, which does not carry the same STA requirement"],"exampleFix":"// before\nTask.Run(() => new DropShadowBitmapEffect()); // MTA thread, throws\n\n// after\nApplication.Current.Dispatcher.Invoke(() =>\n{\n    var effect = new DropShadowBitmapEffect(); // STA UI thread\n});","handlingStrategy":"try-catch","validationCode":"static bool CanCreateBitmapEffect() =>\n    Thread.CurrentThread.GetApartmentState() == ApartmentState.STA;","typeGuard":null,"tryCatchPattern":"try { var effect = new DropShadowBitmapEffect(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"STA\"))\n{\n    // retry on the STA UI thread via Dispatcher.Invoke\n}","preventionTips":["Create all WPF visuals/effects on the Dispatcher/UI thread","Mark entry points [STAThread]; set ApartmentState.STA on threads that touch WPF","Never construct effects inside Task.Run or thread-pool callbacks","Prefer the modern Effect/PixelShader API for new code"],"tags":["wpf","threading","sta","bitmapeffect"],"backgroundTag":"unsupported-platform","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}