Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syncronizing actions in Silverlight

I have a Silverlight app that uses actions to get data from the model (which again gets the data from WCF services).

I need to somehow sync two ActionCallbacks, or wait for them, and then execute some code.

Example:

_model.GetMyTypeList(list =>
{
    MyTypeList.AddRange(list);
});

_model.GetStigTypeList(list =>
{
    StigTypeList.AddRange(list);
});

doSomethingWhenBothHaveReturned();

I know I can use a counter to keep track of how many has returned, but is there not a better way to do this?

EDIT: user24601 has a good answer, but CountdownEvent does not exist in silverlight, any other great ideas? :)

like image 409
randoms Avatar asked Jan 16 '12 12:01

randoms


1 Answers

Yes, a counter is what you need. The 'more elegant' solution would be to use a countdown event:

using (CountDownEvent countDownEvent = new CountDownEvent(2))
{
    _model.GetMyTypeList(list =>
    {
        MyTypeList.AddRange(list);
        countDownEvent.Signal();
    });

    _model.GetStigTypeList(list =>
    {
        StigTypeList.AddRange(list);
        countDownEvent.Signal();
    });

    countdownEvent.Wait();
    doSomethingNowThatWereComplete();
}
like image 132
DanTheMan Avatar answered Sep 23 '22 18:09

DanTheMan