views:

46

answers:

1

I have a collection

ObservableCollection<string> outoverList

And i have a function which call collection

outoverList.Add("out:"+element.tagName);

Function call collection a few times, but sometimes collection lost elements. We call a function -> function adds element -> collection has 9 elements(for example) -> in the next function calling collection has only 8 elements. One element be missing.

Here Resharpers Find usages log:

   Search target
  FindElementHandler.outoverList:ObservableCollection<string>
Found 3 usages in solution
  <FindElementExperiments> (3 items)
    FindElementHandler.cs (3 items)
      (50,13) outoverList = new ObservableCollection<string>();
      (94,13) outoverList.Add("out:"+element.tagName);
      (118,13) outoverList.Add("over:" + element.tagName);

As you can see i just add elements to collection everywhere. i havent remove elements code.

Maybe i did something wrong you can look at screen capture: http://www.youtube.com/watch?v=Ei6dQnHCMIc

I am newbie and often encounter with various problems but this bug looks mystic for me.

P/S/ Sorry for english

+1  A: 

Two likely possibilities occur to me:

  1. you have some other code (perhaps an event handler responding to the observable collection's events, or perhaps another thread) that is explicitly calling remove. To check this, I would add some debug code that subscribes to the observable collection and writes a trace line when items are added removed - that should answer it pretty quickly
  2. you have multiple threads, and a thread-race situation is happening. Note that this collection is not synchronized, so if multiple threads change it (add/remove/re-assign) at the same time, all bets are off and it is entirely possible to lose data, especially with Add. To check this, I would use the same trace handler as above, but I would include (in the output Thread.CurrentThread.ManagedThreadId)
Marc Gravell