dotnet/wpf · error · NotSupportedException

SR.CalendarCollection_MultiThreadedCollectionChangeNotSuppor…

Error message

SR.CalendarCollection_MultiThreadedCollectionChangeNotSupported

What it means

CalendarBlackoutDatesCollection (like other WPF calendar collections) enforces single-threaded access via its owning Dispatcher; ClearItems throws NotSupportedException when called from a thread other than the Calendar's UI thread.

Solutions

  1. Marshal collection mutations back to the UI thread with Dispatcher.Invoke/BeginInvoke
  2. Update the collection in the continuation on the captured UI SynchronizationContext
  3. Compute data off-thread, apply it on the UI thread

Example fix

// before
await LoadBlackoutsAsync();
blackoutDates.Clear();
// after
await LoadBlackoutsAsync();
await dispatcher.InvokeAsync(() => blackoutDates.Clear());
Defensive patterns

Strategy: validation

Validate before calling

if (!calendar.Dispatcher.CheckAccess())
{
    calendar.Dispatcher.Invoke(() => calendar.BlackoutDates.Clear());
    return;
}
calendar.BlackoutDates.Clear();

Type guard

bool OnUiThread(Calendar c) => c.Dispatcher.CheckAccess();

Try / catch

try
{
    calendar.BlackoutDates.Clear();
}
catch (NotSupportedException)
{
    calendar.Dispatcher.Invoke(() => calendar.BlackoutDates.Clear());
}

Prevention

When it happens

Trigger: Calling BlackoutDates.Clear() from a background/worker thread or Task; modifying the collection inside an async continuation without marshaling back to the dispatcher.

Common situations: Loading blackout dates from a database or web service in a background task and clearing/assigning the collection on that thread; async event handlers that forgot Dispatcher.Invoke.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/CalendarBlackoutDatesCollection.cs:158

            } while (currentDate != null && ((range = GetContainingDateRange((DateTime)currentDate)) != null));



            return currentDate;
        }

        #endregion Public Methods

        #region Protected Methods

        /// <summary>
        /// All the items in the collection are removed.
        /// </summary>
        protected override void ClearItems()
        {
            if (!IsValidThread())
            {
                throw new NotSupportedException(SR.CalendarCollection_MultiThreadedCollectionChangeNotSupported);
            }

            foreach (CalendarDateRange item in Items)
            {
                UnRegisterItem(item);
            }

            base.ClearItems();
            this._owner.UpdateCellItems();
        }

        /// <summary>
        /// The item is inserted in the specified place in the collection.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="item"></param>
        protected override void InsertItem(int index, CalendarDateRange item)
        {

View on GitHub (pinned to 81131a70a4)