-5

I have got the following problem.

I have two ObservableCollections coll1, coll2. When adding to the first ObservableCollection coll1 I add in the coll1_CollectionChanged some manipulated data into the coll2 - this fires up the coll2_CollectionChanged.

According to the Action (Add, Remove, Reset) i let the coll2_CollectionCHanged call a function. The issue is, that this function (let me call it func1) starts another asynchronous function(a query). After the query call, the function func1 should do another actions - updating some data.

The problem is, that the coll2_CollectionChanged EventHandler should wait until the func1 is ready (including the query), but it doesn't. All in all there are three Add Actions to the coll2_CollectionCHanged and after each Add the EventHandler should wait for the func1 get ready.

The query in the func1 is an asynchronous function. I marked the func1 as async and call the query with await. Than I let the EventHandler call the func1 also as async - I make the EventHandler async and call the func1 with await. But it doesn't wait.

CAS
  • 535
  • 1
  • 8
  • 15
N.Zukowski
  • 600
  • 1
  • 12
  • 31

1 Answers1

0

But it doesn't wait.

Sure it does. The event handler is paused, waiting for func1 to complete. While it's paused, it returns to its caller - the central point of async.

The behavior you want is to have the code raising the ObservableCollection.CollectionChanged event to (asynchronously) wait for the event handler to complete. This is not easy; trying to twist the built-in ObservableCollection class to support this would be very difficult (maybe impossible).

A far easier solution is to change your logic so that, say, your other code will asynchronously wait for func1 so that it knows all side effects have been applied.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810