dotnet/wpf · error · InvalidOperationException
SR.MediaElement_CannotSetSourceOnMediaElementDrivenByClock
Error message
SR.MediaElement_CannotSetSourceOnMediaElementDrivenByClock
What it means
AVElementHelper.MemberOnInvalidateSource throws this InvalidOperationException when the Source property of a MediaElement is changed while the element is driven by a MediaClock. A clock-driven MediaElement has its timeline owned by the clock, so setting Source directly is not allowed.
Solutions
- Stop using the clock (e.g. remove the MediaClock/ClockController) before changing Source, or create a new MediaElement.
- Control playback exclusively through ClockController when using clocks, and set the source at clock creation time.
- Guard Source updates with a check that the element is not clock-driven.
Example fix
// before
media.Clock = clock;
media.Source = new Uri("other.wmv"); // throws
// after
media.Clock = null; // detach clock first
media.Source = new Uri("other.wmv"); Defensive patterns
Strategy: try-catch
Validate before calling
if (media.Clock != null) throw new InvalidOperationException("Detach the MediaClock before changing Source"); Type guard
bool IsClockDriven(MediaElement m) => m.Clock != null;
Try / catch
try { media.Source = newUri; } catch (InvalidOperationException) { media.Clock = null; media.Source = newUri; } Prevention
- Do not mix ClockController-driven playback with Source property changes
- Set the source when creating the clock; update via ClockController instead
- Null out media.Clock before rebinding Source
When it happens
Trigger: Assigning mediaElement.Source (or a binding/style updating Source) on a MediaElement whose Clock was set via ClockController or MediaClock assignment.
Common situations: Mixing clock-based playback (Storyboard/MediaClock with ClockController.Seek/Play) with source updates; data-binding Source on a clock-controlled element; style triggers that change Source during clock playback.
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
- Animation_Invalid_DefaultValue
- Cannot remove signature from read-only file.
- Image_EncoderNoColorContext
- Image_EncoderNoGlobalMetadata
- Image_EncoderNoGlobalThumbnail
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6ae76361b93582b5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/AVElementHelper.cs:721
/// <summary>
/// Raised when source is changed
/// </summary>
internal static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.IsASubPropertyChange)
{
return;
}
AVElementHelper aveh = AVElementHelper.GetHelper(d);
aveh.MemberOnInvalidateSource(e); // call non-static
}
private void MemberOnInvalidateSource(DependencyPropertyChangedEventArgs e)
{
if (_clock._value != null)
{
throw new InvalidOperationException(SR.MediaElement_CannotSetSourceOnMediaElementDrivenByClock);
}
_source._value = (Uri)e.NewValue;
_source._wasSet = _source._isSet = true;
HandleStateChange();
}
/// <summary>
/// Raised when there is a error with media playback
/// </summary>
private
void
OnMediaFailed(
object sender,
ExceptionEventArgs args
)
{View on GitHub (pinned to 81131a70a4)