WCF方法“异步调用”的“同步问题”
在“WCF”中服务方法的调用是“异步”的,而这些异步的方法的执行顺序有先有后,只要当一部分“异步通信”全部完成之后,才能进行下一步的操作,如何使异步通信同步起来?有两种方法,一种是异步用法逐步执行,一种是通过“标志变量”协调执行。不知您是否有其它建议?
1、顺序执行
方法1_Async
方法1_Completed
方法2_Async
方法2_Completed
BindChart()
2、标志变量,协调执行
int flag=0;
private void BindBaseDatas()
{
string weburl = Application.Current.Host.Source.ToString();
weburl = weburl.Substring(0, (weburl.Length - 23)) + "/ChartsService.svc";
MyChartsService.ChartsServiceClient client = new MyChartsService.ChartsServiceClient("CustomBinding_ChartsService1", weburl);
client.GetMonitoringDatasEntityAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, true);
client.GetMonitoringDatasEntityCompleted += new EventHandler<GetMonitoringDatasEntityCompletedEventArgs>(client_GetMonitoringDatasEntityCompleted);
client.GetMonitoringDatasEntityTwoAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, false);
client.GetMonitoringDatasEntityTwoCompleted += new EventHandler<GetMonitoringDatasEntityTwoCompletedEventArgs>(client_GetMonitoringDatasEntityTwoCompleted);
}
void client_GetMonitoringDatasEntityTwoCompleted(object sender, GetMonitoringDatasEntityTwoCompletedEventArgs e)
{
monitoringstwo = e.Result;
if(flag==0)
{
flag=2;
}
else
{
BindChart();
flag=0;
}
}
void client_GetMonitoringDatasEntityCompleted(object sender, GetMonitoringDatasEntityCompletedEventArgs e)
{
monitoringsone = e.Result;
if(flag==0)
{
flag=3;
}
else
{
BindChart();
flag=0;
}
}
{
string weburl = Application.Current.Host.Source.ToString();
weburl = weburl.Substring(0, (weburl.Length - 23)) + "/ChartsService.svc";
MyChartsService.ChartsServiceClient client = new MyChartsService.ChartsServiceClient("CustomBinding_ChartsService1", weburl);
client.GetMonitoringDatasEntityAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, true);
client.GetMonitoringDatasEntityCompleted += new EventHandler<GetMonitoringDatasEntityCompletedEventArgs>(client_GetMonitoringDatasEntityCompleted);
client.GetMonitoringDatasEntityTwoAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, false);
client.GetMonitoringDatasEntityTwoCompleted += new EventHandler<GetMonitoringDatasEntityTwoCompletedEventArgs>(client_GetMonitoringDatasEntityTwoCompleted);
}
void client_GetMonitoringDatasEntityTwoCompleted(object sender, GetMonitoringDatasEntityTwoCompletedEventArgs e)
{
monitoringstwo = e.Result;
if(flag==0)
{
flag=2;
}
else
{
BindChart();
flag=0;
}
}
void client_GetMonitoringDatasEntityCompleted(object sender, GetMonitoringDatasEntityCompletedEventArgs e)
{
monitoringsone = e.Result;
if(flag==0)
{
flag=3;
}
else
{
BindChart();
flag=0;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步