Reputation: 14511
I have a method that updates my observableCollection
public void UpdateBeat()
{
SequenceCollection = new ObservableCollection<Sequence>();
Random random = new Random();
int randomNumber = random.Next(0, 100);
SequenceCollection.Add(new Sequence(1, 2));
}
I have 2 different methods fired from events - the view updates from 1 of the methods but not the other.
//Does not work
private void BsOnUpdateStep(object sender, EventArgs eventArgs)
{
Console.WriteLine("BS Update");
UpdateBeat();
}
//Works
void total_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
Console.WriteLine("ASIO Written");
UpdateBeat();
}
I have no idea what the difference could be here. The only thing I can tell is that the 1st method fires more often than the 2nd. I cannot get the 2nd to work at all.
Upvotes: 0
Views: 122
Reputation: 7083
I suppose your calls to UpdateBeat are from different threads but ObservableCollection is not thread safe, that is why -probably- you have such strange results.
You should look for an concurrent ObservableCollection.
One such implementation can be found here: http://www.codeproject.com/Tips/414407/Thread-Safe-Improvement-for-ObservableCollection
Upvotes: 2
Reputation: 117064
Try this:
private SequenceCollection = new ObservableCollection<Sequence>();
Random random = new Random();
public void UpdateBeat()
{
int randomNumber = random.Next(0, 100);
SequenceCollection.Add(new Sequence(1, 2));
}
I put the Random
instantiation outside of the method too as you should only instantiate this once to get a proper stream of random numbers.
Upvotes: 1