Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8625039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:40:59+00:00 2026-06-12T07:40:59+00:00

I recently stumbled across an issue in silverlight with using the datacontext changed event.

  • 0

I recently stumbled across an issue in silverlight with using the datacontext changed event.

If you subscribe to a changed event and then immediately unsubscribe it will throw an exception,

DataContextChanged += MainPage_DataContextChanged;
void MainPage_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
  var vm = e.NewValue as VM;
  if(vm != null)
  {
     DataContextChange-= MainPage_DataContextChanged;//throws invalidoperationexception for collection modified
  }
}

to fix this I just unsubscribe the event later, in this situation the requirement is to unsubscribe sooner rather than later so this works.

DataContextChanged += MainPage_DataContextChanged;
void MainPage_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
  var vm = e.NewValue as VM;
  if(vm != null)
  {
      //forces item onto the dispatcher queue so anything needing to happen with 'collections' happens first
      Dispatcher.BeginInvoke(()=>
        {
     DataContextChange-= MainPage_DataContextChanged;//throws invalidoperationexception for collection modified
         });
  }
}

I’m guessing the collections are the Child elements of all the different controls in the visual tree, and I’m guessing their updates are probably happening on the dispatcher queue so my question is this:

Why does the event being unsubscribed after it has fired affect collections that are going to be modified or updated after this?

EDIT:
After giving this some thought Could this have anything to do with the event handlers invocation list being modified before its finished?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:41:01+00:00Added an answer on June 12, 2026 at 7:41 am

    Your suspicions about the invocation list being modified are correct.

    Here is the code that fires the DataContextChanged event, according to dotPeek’s decompilation:

    private void RaisePublicDataContextChanged()
    {
      if (this._dataContextChangedInfo == null)
        return;
      object oldValue = this._dataContextChangedInfo.OldValue;
      object dataContext = this.DataContext;
      if (oldValue == dataContext)
        return;
      this._dataContextChangedInfo.OldValue = dataContext;
      List<DependencyPropertyChangedEventHandler>.Enumerator enumerator = this._dataContextChangedInfo.ChangedHandlers.GetEnumerator();
      try
      {
        // ISSUE: explicit reference operation
        while (((List<DependencyPropertyChangedEventHandler>.Enumerator) @enumerator).MoveNext())
        {
          // ISSUE: explicit reference operation
          ((List<DependencyPropertyChangedEventHandler>.Enumerator) @enumerator).get_Current()((object) this, new DependencyPropertyChangedEventArgs(FrameworkElement.DataContextProperty, oldValue, dataContext));
        }
      }
      finally
      {
        enumerator.Dispose();
      }
    }
    

    As you can see, the code is using an enumerator to iterate through the collection of handlers. So when you unsubscribe from the event during the invocation of a handler, you are invalidating the enumerator, causing the exception you are seeing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently stumbled across the ability to use boomarks in Visual Studio. ( http://www.codeproject.com/Articles/42973/Using-Bookmark-in-Visual-Studio.aspx
I have recently stumbled across an issue where the WPF ListView control seems to
I recently stumbled across a difficult problem.. I'm normally using a NSFetchedResultsController to get
I recently stumbled across an article that claims Microsoft is banning the memcpy() function
I've recently stumbled across geb and it looks like a good way to perform
I was recently looking to practice utilizing dictionaries in python and stumbled across these
I recently started looking into building web applications using .NET MVC and I stumbled
I recently stumbled across DBslayer ( http://code.nytimes.com/projects/dbslayer/wiki/WhyUseIt ) and wondered what is the actual
we've recently stumbled across the excellent Dispatch for ASP FTP deployment plug in. It
I recently stumbled across the following assembly instruction sequence: rep stos dword ptr [edi]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.