我有一个WCF服务并在我的silverlight 4应用程序中使用它。我有5个异步调用。我怎么知道他们已经完成了???
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
}
以外我有:
Prediction p = new Prediction();
p.Get_Data(high, low, open, close, volume, date);
p.Predict();
所以..请问我怎么知道?????
答案 0 :(得分:1)
注册回调,处理异步流程已完成的事实。
但不是“火与忘”的异步的全部意义吗?如果您过多地依赖于与呼叫者的通信,也许您应该切换到同步呼叫并重新考虑您的设计。
答案 1 :(得分:1)
您应该将名为Get_DataCompleted的事件添加到您的Prediction类。
在调用所有service_ * Comlpeted方法时引发此事件。为此,您可以在方法的末尾添加一个调用int,并在int = 0时调用Get_DataCompleted。 在Get_Data的开头将int设置为5.
当调用Get_Data并且int为&gt;时,您可能会抛出InvalidOperationException。 0.(或中止之前的通话,重新初始化,......)
在引发Get_DataCompleted时调用p.Predict。
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
}
外面:
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();
}