locked
Can I add elements to instance of ObservableCollection from TPL task? RRS feed

  • Question

  • Hi!

    I develop an application with TPL in .NET Framework 4.0. My question is the question of beginner in TPL. It's the following.

    Can I add elements to instance of ObservableCollection from TPL task or there are any specific observable collections which are dedicated to use from TPL tasks? If such specific observable collections exist, please give me reference to download one of them if it is possible.


    Sunday, July 7, 2013 6:08 AM

Answers

  • I solve the problem in next manner. In the FuturesTradeViewModel class that represents of view model part of application I defined Dispatcher instance:

    private Dispatcher _myDispatcher;

    Then I define the following method in ViewModel class:

    private void AddRecordToFutSessContentsObject objPar)
    {
         FutSessContentsRecord newRecord = objPar as FutSessContentsRecord;
         FuturesTradeViewModel.FutSessContents_collection.Add(newRecord);
    }
    where FutSessContents_collection is ObservableCollection and FutSessContentsRecord is the class of elements in this collection.

    Then in TPL-task I write:

    // Create new element to insert to ObservableCollection.
                                            FutSessContentsRecord newRecord = new FutSessContentsRecord();
    // Assign values to the element's fields.
    newRecord.IsinId = futSessContents.isin_id;
    newRecord.Isin = futSessContents.isin;
    newRecord.ShortIsin = futSessContents.short_isin;
    newRecord.InstrumentName = futSessContents.name;
    newRecord.LastClQuote = futSessContents.last_cl_quote;
    // Add element to the ObservableCollection.
    this.myDispatcher.BeginInvoke(DispatcherPriority.Normal,
                                   new OneArgDelegate(AddRecordToFutSessContents), newRecord);
    This works.
    • Marked as answer by TownSparrow Tuesday, July 9, 2013 8:19 AM
    • Edited by TownSparrow Tuesday, July 9, 2013 8:20 AM corrected
    Monday, July 8, 2013 5:39 AM

All replies

  • You can add to ObservableCollection from a background thread (which is where Tasks run by default). The question is whether the code that watches for the changes will be able to handle that.

    If you're using WPF in .Net 4.5, you can use BindingOperations.EnableCollectionSynchronization() to make this work. If you're using WPF in .Net 4.0, there is no way to make that work and you will have to Dispatch the collection change to the UI thread by yourself.

    Sunday, July 7, 2013 10:54 AM
  • I solve the problem in next manner. In the FuturesTradeViewModel class that represents of view model part of application I defined Dispatcher instance:

    private Dispatcher _myDispatcher;

    Then I define the following method in ViewModel class:

    private void AddRecordToFutSessContentsObject objPar)
    {
         FutSessContentsRecord newRecord = objPar as FutSessContentsRecord;
         FuturesTradeViewModel.FutSessContents_collection.Add(newRecord);
    }
    where FutSessContents_collection is ObservableCollection and FutSessContentsRecord is the class of elements in this collection.

    Then in TPL-task I write:

    // Create new element to insert to ObservableCollection.
                                            FutSessContentsRecord newRecord = new FutSessContentsRecord();
    // Assign values to the element's fields.
    newRecord.IsinId = futSessContents.isin_id;
    newRecord.Isin = futSessContents.isin;
    newRecord.ShortIsin = futSessContents.short_isin;
    newRecord.InstrumentName = futSessContents.name;
    newRecord.LastClQuote = futSessContents.last_cl_quote;
    // Add element to the ObservableCollection.
    this.myDispatcher.BeginInvoke(DispatcherPriority.Normal,
                                   new OneArgDelegate(AddRecordToFutSessContents), newRecord);
    This works.
    • Marked as answer by TownSparrow Tuesday, July 9, 2013 8:19 AM
    • Edited by TownSparrow Tuesday, July 9, 2013 8:20 AM corrected
    Monday, July 8, 2013 5:39 AM