Asynchronous Modification of ObservableCollections

I am having problems updating the binding of a ListView WPF to an ObservableCollection in a task using (parallel task library)

I have a small tool that reads Edifact Files and displays the amount of each segment (the first three letters of the line).

The contained segments with their counts are displayed in the list.

Wenn I initially upload the file, everything works fine, and I see a GUI counting the segments. My program allowed me to switch to another file, if I do this (using exactly the same code), it does not fulfill the following exceptions.

This type of CollectionView does not support changes to the SourceCollection from a stream other than the Dispatcher stream.

Here is the code that fails

public class SegementLineCollection : ObservableCollection<SegmentLine>
  {
    public void IncrementCount(string Segment)
    {
      Segment = Segment.ToUpper();

      SegmentLine line;
      if (this.Select(x => x.SegmentName).Contains(Segment))
      {
        line = this.Where(x => x.SegmentName == Segment).FirstOrDefault();
      }
      else
      {
        line = new SegmentLine();
        line.SegmentName = Segment;

        this.Add(line); // <--- At this point comes the Exception
      }

      line.Count++;
    }
  }

TPL, :

private string _FileName;
    public string FileName
    {
      get
      {
        return _FileName;
      }
      set
      {
        _FileName = value;
        OnPropertyChanged("FileName");

        if (!String.IsNullOrEmpty(value)) 
          new Task(() => StartFile()).Start();
      }
    }

- ?

------------ E D T ------------------

TaskScheduler.FromCurrentSynchronizationContext() Dispatcher Trick!

, .

, , Reader Onject ViewModel, - INotifyPropertyChanged

+3
4

:

if (Dispatcher.CurrentDispatcher.CheckAccess())
  this.Add(...)
else
  Dispatcher.CurrentDispatcher.Invoke(() => this.Add(...));
+1

IncrementCount .

TPL TaskScheduler.FromCurrentSynchroniztionContext() .

var task = new Task<string>(() => { return "segment"; })
var task2 = task.ContinueWith(t => IncrementCount(t.Result),
                              TaskScheduler.FromCurrentSynchroniztionContext());
task.Start();
+1

, Dispatcher.BeginInvoke , UI

0

I have a solution for such problems in the next blog.

http://bathinenivenkatesh.blogspot.co.uk/2011/07/wpf-build-more-responsive-ui.html

he got a detailed explanation and code snippets ...

-2
source

All Articles