{"record":{"id":"df0e9ef86230ac76","repo":"dotnet/wpf","slug":"sr-image-badversion","errorCode":null,"errorMessage":"SR.Image_BadVersion","messagePattern":"SR\\.Image_BadVersion","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaPlayerState.cs","lineNumber":985,"sourceCode":"        /// are being called from the correct thread. This method should\n        /// be the first thing called from any internal method.\n        /// </summary>\n        private void VerifyAPI()\n        {\n            //\n            // We create _nativeMedia in the constructor, so it should always\n            // be initialized.\n            //\n            Debug.Assert(_nativeMedia != null && !_nativeMedia.IsInvalid);\n\n            //\n            // We only allow calls to any media object on the UI thread.\n            //\n            _dispatcher.VerifyAccess();\n\n            if (_nativeMedia == null || _nativeMedia.IsInvalid)\n            {\n                throw new System.NotSupportedException(SR.Image_BadVersion);\n            }\n        }\n\n        /// <summary>\n        /// Verifies that this player is not currently controlled by a clock. Some actions are\n        /// invalid while we are under clock control.\n        /// </summary>\n        private\n        void\n        VerifyNotControlledByClock()\n        {\n            if (Clock != null)\n            {\n                throw new InvalidOperationException(SR.Media_NotAllowedWhileTimingEngineInControl);\n            }\n        }\n\n        /// <summary>","sourceCodeStart":967,"sourceCodeEnd":1003,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaPlayerState.cs#L967-L1003","documentation":"MediaPlayerState.VerifyAPI ensures calls are made on the UI thread AND that the underlying native media object (_nativeMedia) still exists and is valid. If the native media handle is null or has been released (IsInvalid), any state query such as IsBuffering, CanPause, DownloadProgress, BufferingProgress, NaturalVideoHeight, or NaturalVideoWidth throws NotSupportedException (message resource SR.Image_BadVersion).","triggerScenarios":"Querying any MediaPlayerState property (IsBuffering, CanPause, DownloadProgress, BufferingProgress, NaturalVideoHeight, NaturalVideoWidth) after the native media player has been closed, disposed, or before a media source was successfully opened.","commonSituations":"Reading NaturalVideoWidth/NaturalVideoHeight before MediaOpened fires, polling DownloadProgress after Close() or unloading the MediaElement, or accessing state from event handlers after the player was torn down.","solutions":["Guard state queries: only read properties after MediaOpened and before Close/teardown","Handle NullReference/NotSupported by checking player.Source and MediaElement state (e.g. mediaElement.Source != null && mediaLoaded) first","Subscribe to MediaClosed/Unloaded and stop polling timers that read these properties","Reopen the media if you need the state after it was closed"],"exampleFix":"// before\ndouble h = player.NaturalVideoHeight; // throws if native media gone\n// after\nif (player != null && player.HasVideo && player.Source != null)\n{\n    double h = player.NaturalVideoHeight;\n}","handlingStrategy":"validation","validationCode":"bool CanQueryState(MediaElement me) => me != null && me.Source != null && me.Clock == null && (me.CurrentState == MediaState.Play || me.CurrentState == MediaState.Pause || me.CurrentState == MediaState.Stop);","typeGuard":"bool HasValidNativeMedia(MediaPlayer p) => p != null && p.Source != null && !p.IsDisposed;","tryCatchPattern":"try { var h = player.NaturalVideoHeight; } catch (NotSupportedException) { h = 0; /* native media unavailable */ }","preventionTips":["Only read media state properties after MediaOpened fires","Stop polling timers on MediaClosed/Unloaded","Never cache MediaPlayerState across Close()/teardown","Re-check Source != null before each query"],"tags":["wpf","media","invalid-state"],"backgroundTag":"unsupported-operation","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"}