Whilst the synchronising discussion is interesting in itself, perhaps
the original problem could be solved by batching your logically grouped
operations.  Assuming you 'own' the services, you could create less
granular methods which return aggregated 'datacontracts'.

 

A variation of this idea is implemented quite nicely in ADO.NET data
services, by supplying multiple DataServiceRequest to
DataServiceContext.BeginExecuteBatch you get better performance and
don't have to synchronise the callbacks. 

 

 (I'm only using that as an example by the way, as you can't call custom
service methods within a batch within that framework.)

 

From: Miguel Madero [mailto:m...@miguelmadero.com] 
Sent: Wednesday, 12 August 2009 11:04 PM
To: ozsilverlight@ozsilverlight.com
Subject: Re: Wait for multiple Async calls to finish (SL3)

 

Asheesh, 

I'm comparing the different approaches, the one in StackOverFlow works
well, this is what I don't like:
* I think it's hard to work with multiple services using events instead
of callbacks (as in my example), 
* They're using an AutoResetEvent, which we get for free if we use the
callbacks, so that means less things to set up. 
* They're using a BackgroundWorker and a Thread 
* They're using an event to notify, which is really not necessary
(altough a clear). We could directly do whatever we need after the
WaitOne or after calling Start (instead of raising the event). 
* They're starting the BackgroundWorker and leave it there just waiting,
this should be done only just before calling the services. 
* The event should be raised on the UI Thread

I did some changes to that sample. I can send it to you as a reference. 
I don't think it would make a huge difference in performance, since any
perf hit due to thread synchronization logic would be nothing compare to
multiple service calls. However, I've not had a chance to do any perf
testing. 


BTW. I think you forgot to use the BackgroundWorker and that's why when
you tried to use this approach the application was locked on the first
WaitOne since it was done in the UI Thread (just as the even returning
from the ServiceCall). 




On Wed, Aug 12, 2009 at 10:24 PM, Miguel Madero <m...@miguelmadero.com>
wrote:

Attached is a brief example on how to do this using the WaitHandle in
the IAsyncResult returned by each async method. 

The code is simple. In Silverlight I just do 10 service call that will
add an item to a ListBox. I'll wait until all the service calls end to
add another message to the list (this has to run in a different thread
to avoid blocking the UI). Also note that adding items to the list have
to be done through the Dispatcher sicne they will modify the UI.
There're a bunch of lamdas, but it's easy to follow. 


 public MainPage()
        {
            InitializeComponent();
            var results = new ObservableCollection<string>();
            var asyncResults = new List<IAsyncResult>();
            resultsList.ItemsSource = results;
            var service = new Service1Client() as Service1;
            
            1.To(10).Do(i=>
                asyncResults.Add(service.BeginDoWork(ar => 
                    Dispatcher.BeginInvoke(() =>
results.Add(String.Format("Call {0} finished: {1}", i,
service.EndDoWork(ar)))), 
                    null))
            );

            new Thread(()=>
            {
                asyncResults.ForEach(a => a.AsyncWaitHandle.WaitOne());
                Dispatcher.BeginInvoke(() => results.Add("Everything
finished"));
            }).Start();
        }

Just to help with the testing, this is the service

public class Service1
    {
        private const int maxMilliSecs = 500;
        private const int minMillisSecs = 100;
        [OperationContract]
        public int DoWork()
        {
            int millisSecsToWait = new Random().Next(maxMilliSecs -
minMillisSecs) + minMillisSecs;
            Thread.Sleep(millisSecsToWait);
            return millisSecsToWait;
        }
    }




On Wed, Aug 12, 2009 at 9:21 PM, Miguel Madero <m...@miguelmadero.com>
wrote:

        Ali is going to give a talk on the Rx framework at the next SDDN
Event in Sydney :)

        On Wed, Aug 12, 2009 at 7:48 PM, Jordan Knight
<jordan.kni...@readify.net> wrote:

                I had a quick look at this the other day and when I
compiled up the reactive dll, the classes were different that in the
example:

                 

                ObservableExtensions

                        DoAsync

                Didn't exist?

                 

                I didn't really examine where I was going wrong in any
great depth, so If you have any success I'd be interested to hear about
it :)

                 

                 

                Jordan Knight 
                Readify | Senior Consultant
                Lead Trainer - Rich Interactive Media 

                Suite 408 LifeLabs Building | 198 Harbour Esplanade |
Docklands | VIC 3008 | Australia 

                M: +61 403 532 404 | E: jordan.kni...@readify.net | W:
http://www.readify.net | B: http://blog.webjak.net 

                 

                 

                 

                 

                From: ozsilverlight@ozsilverlight.com
[mailto:ozsilverli...@ozsilverlight.com] On Behalf Of Jake Ginnivan

                
                Sent: Wednesday, 12 August 2009 6:32 PM
                To: OzSilverlight@ozsilverlight.com
                Subject: RE: Wait for multiple Async calls to finish
(SL3)

                 

        
http://themechanicalbride.blogspot.com/2009/07/introducing-rx-linq-to-ev
ents.html

                 

                Have a look at this, the Silverlight team use it in
their unit testing. I still have not used it yet, but really keen to
dive in and use this stuff.

                 

                Jake Ginnivan

                IT Coordinator

                 

                ioGlobal Pty Ltd.

                ISO 9001 Quality Certified

                Resource Analytics & Data Systems Automation

                T:  +61 8 6555 6510

                F:  +61 8 6311 3256

                M:  +61 403 846 400

                E:  jake.ginni...@ioglobal.net

                www.ioglobal.net <http://www.ioglobal.net/> 

                 

                From: OzSilverlight@ozsilverlight.com
[mailto:ozsilverli...@ozsilverlight.com] On Behalf Of Asheesh Soni
                Sent: Wednesday, 12 August 2009 3:49 PM
                To: OzSilverlight@ozsilverlight.com
                Subject: Wait for multiple Async calls to finish (SL3)

                 

                Any one knows a better / cleaner way to do this than:
                
                http://silverlight.net/forums/t/12437.aspx
                http://silverlight.net/forums/t/72631.aspx
                
                I have already dumped daisy-chaining of calls in favor
of multiple async calls with a boolean flag for each. 
                After calling all the async operations, I am using a
DispatcherTimer ticker event to poll if all the flags have been set.
                Works like a charm, and a huge improvement in
performance and readability over the chaining. But still not the perfect
solution.
                
                I couldn't get this one to work:
        
http://stackoverflow.com/questions/811855/threading-multiple-async-calls
-silverlight
                The thread sleeps for ever on the 1st .waitone call.
                
                Any ideas?

                
________________________________


                Support procedure: https://www.codify.com/lists/support
                List address: ozsilverlight@ozsilverlight.com
<mailto:ozsilverlight@ozsilverlight.com> 
                Subscribe: ozsilverlight-subscr...@ozsilverlight.com
<mailto:ozsilverlight-subscr...@ozsilverlight.com> 
                Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
<mailto:ozsilverlight-unsubscr...@ozsilverlight.com> 
                List FAQ: http://www.codify.com/lists/ozsilverlight
<http://www.codify.com/lists/ozsilverlight> 
                Other lists you might want to join:
http://www.codify.com/lists <http://www.codify.com/lists>  

                
________________________________


                Support procedure: https://www.codify.com/lists/support
                List address: ozsilverlight@ozsilverlight.com
<mailto:ozsilverlight@ozsilverlight.com> 
                Subscribe: ozsilverlight-subscr...@ozsilverlight.com
<mailto:ozsilverlight-subscr...@ozsilverlight.com> 
                Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
<mailto:ozsilverlight-unsubscr...@ozsilverlight.com> 
                List FAQ: http://www.codify.com/lists/ozsilverlight
<http://www.codify.com/lists/ozsilverlight> 
                Other lists you might want to join:
http://www.codify.com/lists <http://www.codify.com/lists> 

                
________________________________


                Support procedure: https://www.codify.com/lists/support
                List address: ozsilverlight@ozsilverlight.com
<mailto:ozsilverlight@ozsilverlight.com> 
                Subscribe: ozsilverlight-subscr...@ozsilverlight.com
<mailto:ozsilverlight-subscr...@ozsilverlight.com> 
                Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
<mailto:ozsilverlight-unsubscr...@ozsilverlight.com> 
                List FAQ: http://www.codify.com/lists/ozsilverlight
<http://www.codify.com/lists/ozsilverlight> 
                Other lists you might want to join:
http://www.codify.com/lists <http://www.codify.com/lists>  

        
        
        

        
        -- 
        Miguel A. Madero Reyes
        www.miguelmadero.com (blog)
        m...@miguelmadero.com

        
________________________________


        Support procedure: https://www.codify.com/lists/support
        List address: ozsilverlight@ozsilverlight.com
<mailto:ozsilverlight@ozsilverlight.com> 
        Subscribe: ozsilverlight-subscr...@ozsilverlight.com
<mailto:ozsilverlight-subscr...@ozsilverlight.com> 
        Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
<mailto:ozsilverlight-unsubscr...@ozsilverlight.com> 
        List FAQ: http://www.codify.com/lists/ozsilverlight
<http://www.codify.com/lists/ozsilverlight> 
        Other lists you might want to join: http://www.codify.com/lists
<http://www.codify.com/lists>  




-- 
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
m...@miguelmadero.com

________________________________

Support procedure: https://www.codify.com/lists/support
List address: ozsilverlight@ozsilverlight.com
<mailto:ozsilverlight@ozsilverlight.com> 
Subscribe: ozsilverlight-subscr...@ozsilverlight.com
<mailto:ozsilverlight-subscr...@ozsilverlight.com> 
Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
<mailto:ozsilverlight-unsubscr...@ozsilverlight.com> 
List FAQ: http://www.codify.com/lists/ozsilverlight
<http://www.codify.com/lists/ozsilverlight> 
Other lists you might want to join: http://www.codify.com/lists
<http://www.codify.com/lists>  




-- 
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
m...@miguelmadero.com

________________________________

Support procedure: https://www.codify.com/lists/support
List address: ozsilverlight@ozsilverlight.com
<mailto:ozsilverlight@ozsilverlight.com> 
Subscribe: ozsilverlight-subscr...@ozsilverlight.com
<mailto:ozsilverlight-subscr...@ozsilverlight.com> 
Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
<mailto:ozsilverlight-unsubscr...@ozsilverlight.com> 
List FAQ: http://www.codify.com/lists/ozsilverlight
<http://www.codify.com/lists/ozsilverlight> 
Other lists you might want to join: http://www.codify.com/lists
<http://www.codify.com/lists>  
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________

--------------------------------------------------------------------------------
Support procedure: https://www.codify.com/lists/support
List address: ozsilverlight@ozsilverlight.com
Subscribe: ozsilverlight-subscr...@ozsilverlight.com
Unsubscribe: ozsilverlight-unsubscr...@ozsilverlight.com
List FAQ: http://www.codify.com/lists/ozsilverlight
Other lists you might want to join: http://www.codify.com/lists

Reply via email to