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-events.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
>>
>>
>>
>> *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
>> 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
>>  ------------------------------
>>
>> 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
>>   ------------------------------
>> 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
>>
>
>
>
> --
> Miguel A. Madero Reyes
> www.miguelmadero.com (blog)
> m...@miguelmadero.com
>  ------------------------------
> 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
>



-- 
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
m...@miguelmadero.com
--------------------------------------------------------------------------------
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