{"record":{"id":"2d68659c16ecaece","repo":"dotnet/wpf","slug":"sr-mediasystem-apiinvalidcontext","errorCode":null,"errorMessage":"SR.MediaSystem_ApiInvalidContext","messagePattern":"SR\\.MediaSystem_ApiInvalidContext","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaSystem.cs","lineNumber":282,"sourceCode":"        /// Note that VerifyContext(A) AND AssertSameContext(A, B) implies that VerifyContext(B) holds. Hence you\n        /// don't need to check the context for each argument if you assert the same context.\n        /// </remarks>\n        internal static void AssertSameContext(\n            DispatcherObject reference,\n            DispatcherObject other)\n        {\n            Debug.Assert(reference != null, \"The reference object can not be null.\");\n\n            // DispatcherObjects may be created with the option of becoming unbound.\n            // An unbound DO may be created without a Dispatcher or may detach from\n            // its Dispatcher (e.g., a Freezable).  Unbound DOs return null for their\n            // Dispatcher and should be accepted anywhere.\n            if (other != null &&\n                reference.Dispatcher != null &&\n                other.Dispatcher != null &&\n                reference.Dispatcher != other.Dispatcher)\n            {\n                throw new ArgumentException(SR.MediaSystem_ApiInvalidContext);\n            }\n        }\n\n        /// <summary>\n        /// This flag indicates if the transport has been disabled by a session disconnect.\n        /// If the transport has been disabled we need to defer all media system startup requests\n        /// until we get the next connect message\n        /// </summary>\n        internal static bool IsTransportConnected\n        {\n            get { return s_isConnected; }\n            set { s_isConnected = value; }\n        }\n\n        /// <summary>\n        /// This flag indicates if all rendering should be in software.\n        /// </summary>\n        internal static bool ForceSoftwareRendering","sourceCodeStart":264,"sourceCodeEnd":300,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaSystem.cs#L264-L300","documentation":"MediaSystem.AssertSameContext throws ArgumentException when two media-related objects (a reference and 'other') live on different dispatchers. Media objects are bound to the dispatcher/thread they were created on; passing an object from another UI thread's context is rejected.","triggerScenarios":"Passing a MediaPlayer, MediaCapture/context object, or other media resource created on one UI thread into a media API on a different thread where both have non-null but differing Dispatcher properties.","commonSituations":"Creating MediaPlayer on one window's thread and using it with objects from another window/thread; background-thread media setup then handing the object to the UI thread without recreating it.","solutions":["Create and use all related media objects on the same dispatcher/thread","If cross-thread is needed, marshal via Dispatcher.Invoke/BeginInvoke so operations run on the owning thread","Recreate the media object on the target thread instead of reusing it"],"exampleFix":"// before\nuiThreadMediaPlayer.??? = backgroundCreatedState; // ArgumentException\n// after\nDispatcher.FromThread(uiThread).Invoke(() =>\n{\n    var player = new MediaPlayer(); // create on same thread\n    // use player with other UI-thread media objects\n});","handlingStrategy":"validation","validationCode":"bool SameDispatcher(DispatcherObject a, DispatcherObject b) => a == null || b == null || a.Dispatcher == null || b.Dispatcher == null || a.Dispatcher == b.Dispatcher;","typeGuard":"bool IsOnSameThread<T>(T mediaObj) where T : DispatcherObject => mediaObj.Dispatcher.CheckAccess();","tryCatchPattern":"try { UseMediaObjects(refObj, other); } catch (ArgumentException ex) when (ex.Message.Contains(\"context\")) { RecreateOnCurrentDispatcher(other); }","preventionTips":["Create and consume media objects on one thread","Use Dispatcher.Invoke to hop to the owning thread","Never share MediaPlayer/MediaState objects across windows on different threads"],"tags":["wpf","media","threading"],"backgroundTag":"thread-affinity-violation","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"}