tags:

views:

32

answers:

1

In Viewmodel, I want to track the status of async call. Suppose I have two async calls in viewmodel, I want to track when all async call done. What I did as below: Set to private var to track each async calls:

private bool _isDone1 = false;
private bool _isDone2 = false;

Set one property like:

 private bool _isDone;
 public bool IsDone
        {
            get { return this._isDone1&&this._isDone2; }
            set
            {
                if (this._isDone != value)
                {
                    this._isDone = value;                    
                    if(this._isDone)
                        {
                          // done somting here when all async call done
                        }
                    this.RaisePropertyChanged("IsDone");
                }
            }
        }

In completed event for each async call, set code like: For call 1:

_isDone1 = true;
this.RaisePropertyChanged("IsDone");

For call 2:

_isDone2 = true;
this.RaisePropertyChanged("IsDone");

Then I run the app, It seems code for IsDone never be touched. How to reslove this problem or any better solution for this case?

+1  A: 

Your IsDone property is not well designed. The setter and getter have nothing to do with each other. It's entirely possible at some point for your app to set IsDone to true, but _isDone1 && _isDone2 evaluate to false. This may lead to subtle bugs.

A better general approach when you need all of your async requests to be complete before proceeding is to use a batch loader. It will batch up all your async requests and fire an event when they are all complete. Here is a batch loader written for RIA Services, but if you aren't using RIA Services the general concept still applies.

As for why your "code for IsDone is never touched", it's hard to say from the snippet you've given. You are raising the correct property change name, so there is something else going on in your app. Perhaps post more code?

In the code you posted, you never call the IsDone setter, so as far as we know the if statement inside the setter that is used to signify to yourself you are ready to proceed never gets called.

Matt Greer