Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute async operations sequentially?

Tags:

c#

silverlight

Now I am using something simmilar to this construction

A.Completed += () =>
{ B.Completed += () =>
  {  C.Completed += () =>
     {
       //
     }
     C();
   }
   B();
 }  
 A();

And not very happy with it. Is there a better, more cleaner way to do such kind of sequent/concurrent action execution?

I have a look at Rx framework, but it is designed for other tasks and looks like way overkill for my problem.

like image 737
v00d00 Avatar asked Feb 15 '11 14:02

v00d00


3 Answers

There is no need to nest setting up the Completed events, unless someone might actually call B or C before A is completed. If you separate it out, here is what it looks like:

A.Completed += () => { B(); };
B.Completed += () => { C(); };  
C.Completed += () => { //   }; 

A();

I think removing the nesting actually makes it much cleaner.

like image 139
Chris Pitman Avatar answered Sep 18 '22 21:09

Chris Pitman


Have a look at ReactiveUI. It's based on Rx and will simplify your code a lot. Here is an example: Calling Web Services in Silverlight using ReactiveXaml

like image 23
Giorgi Avatar answered Sep 20 '22 21:09

Giorgi


You can have a look at TPL. You can write code like in this thread: link

like image 39
DaeMoohn Avatar answered Sep 21 '22 21:09

DaeMoohn