Reputation: 73
I have a WCF service and consuming it in my silverlight 4 application. i have 5 async calls. how can i know that they have completed???
void service_StochSlowCompleted(object sender, StochSlowCompletedEventArgs e)
{
int count = e.Result.Count / 2;
for (int i = 0; i < count; i++)
{
Stoch.Add(e.Result[i]);
}
}
void service_MovingAvgCompleted(object sender, MovingAvgCompletedEventArgs e)
{
MA = e.Result;
}
void service_MomentumCompleted(object sender, MomentumCompletedEventArgs e)
{
PMO = e.Result;
}
void service_RSICompleted(object sender, RSICompletedEventArgs e)
{
RSI = e.Result;
}
void service_OBVCompleted(object sender, OBVCompletedEventArgs e)
{
OBV = e.Result;
}
public void Get_Data(ObservableCollection<double> high, ObservableCollection<double> low, ObservableCollection<double> open, ObservableCollection<double> close, ObservableCollection<double> volume, ObservableCollection<DateTime> date)
{
service.OBVAsync(0, close.Count - 1, close, volume);
service.RSIAsync(0, close.Count - 1, close, 9);
service.StochSlowAsync(0, close.Count - 1, high, low, close, 14, 3, 14);
service.MomentumAsync(0, close.Count - 1, close, 10);
service.MovingAvgAsync(0, close.Count - 1, close, 10);
Close = close;
Date = date;
}
public void Predict()
{
//some code uses the results returned from the serivce
}
and outside i have:
Prediction p = new Prediction();
p.Get_Data(high, low, open, close, volume, date);
p.Predict();
So .. please how can i know?????
Upvotes: 2
Views: 737
Reputation: 308813
Register callbacks that deal with the fact that the asynch processes have completed.
But isn't the whole point of asynch to "fire and forget"? If you rely too much on communicating back to the caller, maybe you should switch to a synchronous call and rethink your design.
Upvotes: 1
Reputation: 13138
You should Add an event named Get_DataCompleted to your Prediction class.
Raise this event when all service_*Comlpeted methods have been called. To do that, you may add a call at the end of the method that decrement an int and call raises the Get_DataCompleted when the int = 0. Set the int to 5 at the begining of Get_Data.
You may throw an InvalidOperationException when Get_Data is called and the int is > 0. (Or abort previous calls, reinitialize, ...)
Call p.Predict when the Get_DataCompleted is raised.
public event EventHandler Get_DataCompleted;
private int pendingCalls;
private void OnCompleted()
{
pendingCalls -= 1;
if (pendingCalls == 0 && Get_DataCompleted != null)
Get_DataCompleted(this, EventArgs.Empty);
}
void service_StochSlowCompleted(object sender, StochSlowCompletedEventArgs e)
{
int count = e.Result.Count / 2;
for (int i = 0; i < count; i++)
{
Stoch.Add(e.Result[i]);
}
OnCompleted();
}
void service_MovingAvgCompleted(object sender, MovingAvgCompletedEventArgs e)
{
MA = e.Result;
OnCompleted();
}
void service_MomentumCompleted(object sender, MomentumCompletedEventArgs e)
{
PMO = e.Result;
OnCompleted();
}
void service_RSICompleted(object sender, RSICompletedEventArgs e)
{
RSI = e.Result;
OnCompleted();
}
void service_OBVCompleted(object sender, OBVCompletedEventArgs e)
{
OBV = e.Result;
OnCompleted();
}
public void Get_Data(ObservableCollection<double> high, ObservableCollection<double> low, ObservableCollection<double> open, ObservableCollection<double> close, ObservableCollection<double> volume, ObservableCollection<DateTime> date)
{
if (pendingCalls > 0)
throw new InvalidOperationException();
pendingCalls = 5;
service.OBVAsync(0, close.Count - 1, close, volume);
service.RSIAsync(0, close.Count - 1, close, 9);
service.StochSlowAsync(0, close.Count - 1, high, low, close, 14, 3, 14);
service.MomentumAsync(0, close.Count - 1, close, 10);
service.MovingAvgAsync(0, close.Count - 1, close, 10);
Close = close;
Date = date;
}
public void Predict()
{
//some code uses the results returned from the serivce
}
outside :
Prediction p = new Prediction();
p.Get_Data(high, low, open, close, volume, date);
p.Get_DataCompleted += new EventHandler(p_Get_DataCompleted);
void p_Get_DataCompleted(object sender, EventArgs e)
{
(sender as Prediction).Predict();
}
Upvotes: 1