dotnet/wpf · error · InvalidOperationException

SR.Media_NotAllowedWhileTimingEngineInControl

Error message

SR.Media_NotAllowedWhileTimingEngineInControl

What it means

MediaPlayerState.VerifyNotControlledByClock throws InvalidOperationException when the media player is being driven by a MediaClock/MediaTimeline (Clock != null). While the timing engine controls playback, direct commands like setting Position, SpeedRatio, or calling Open/Play/Pause/Stop are forbidden because the clock owns the timeline.

Solutions

  1. Use the clock's Controller (e.g. clock.Controller.Seek/SpeedRatio/Pause/Resume) instead of player methods when under clock control
  2. Remove the Clock association (e.g. don't create the player from MediaTimeline) if you want imperative control
  3. Check Clock != null before calling player APIs and branch to the clock-controller path

Example fix

// before
player.Position = TimeSpan.FromSeconds(10); // InvalidOperationException when clock-controlled
// after
if (player.Clock != null)
    player.Clock.Controller.Seek(TimeSpan.FromSeconds(10), TimeSeekOrigin.BeginTime);
else
    player.Position = TimeSpan.FromSeconds(10);
Defensive patterns

Strategy: validation

Validate before calling

if (player.Clock != null) { /* use clock.Controller for seek/speed/play-pause */ }

Type guard

bool IsClockControlled(MediaPlayer p) => p.Clock != null;

Try / catch

try { player.Position = pos; } catch (InvalidOperationException ex) when (ex.Message.Contains("Timing") || ex.Message.Contains("clock")) { player.Clock?.Controller?.Seek(pos, TimeSeekOrigin.BeginTime); }

Prevention

When it happens

Trigger: Setting Position or SpeedRatio, or calling Open/Play/Pause/Stop, on a MediaPlayer/MediaElement whose Clock property is non-null - i.e. the player was created via MediaClock (e.g. MediaTimeline with BeginAnimation/Storyboard or Clock = timeline.CreateClock()).

Common situations: Mixing storyboard/clock-driven MediaElement playback with imperative Play/Pause calls in the same control; using MediaTimeline in a Storyboard then trying player.Position = new TimeSpan(...) in a slider handler.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/19f98e7c01893f18. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaPlayerState.cs:999

            _dispatcher.VerifyAccess();

            if (_nativeMedia == null || _nativeMedia.IsInvalid)
            {
                throw new System.NotSupportedException(SR.Image_BadVersion);
            }
        }

        /// <summary>
        /// Verifies that this player is not currently controlled by a clock. Some actions are
        /// invalid while we are under clock control.
        /// </summary>
        private
        void
        VerifyNotControlledByClock()
        {
            if (Clock != null)
            {
                throw new InvalidOperationException(SR.Media_NotAllowedWhileTimingEngineInControl);
            }
        }

        /// <summary>
        /// SendMediaPlayerCommand
        /// </summary>              SecurityNote
        private
        void
        SendMediaPlayerCommand(
            DUCE.Channel            channel,
            DUCE.ResourceHandle     handle,
            bool                    notifyUceDirectly
            )
        {
            // This is an interrop call, but, it does not set a last error being a COM call. 

            //
            // AddRef to ensure the media player stays alive during transport, even if the

View on GitHub (pinned to 81131a70a4)