读书人

“WCF异步通信”同步有关问题

发布时间: 2012-07-29 15:26:13 作者: rapoo

“WCF异步通信”,同步问题
WCF是异步通信,比如一个“通信数据”,依赖于另一个“通信线程”的数据。

第一个取数据的过程:

client.GetAreaHeadCountAsync();

client.GetAreaHeadCountCompleted += new EventHandler<GetAreaHeadCountCompletedEventArgs>(client_GetAreaHeadCountCompleted);

void client_GetAreaHeadCountCompleted(object sender, GetAreaHeadCountCompletedEventArgs e)
{
//var v = e.Result;
AreaHeadCount = e.Result;

GetMonitorDatas();
}



第二个方法:

client.GetMonitoringDatasEntityAsync(monitortype, monitordate_type, monitordate_value, monitortype_type, monitortype_value, meterusetype, issubstation);

client.GetMonitoringDatasEntityCompleted += new EventHandler<GetMonitoringDatasEntityCompletedEventArgs>(client_GetMonitoringDatasEntityCompleted);

void client_GetMonitoringDatasEntityCompleted(object sender, GetMonitoringDatasEntityCompletedEventArgs e)
{
var result = e.Result;

var datas =FormatMonitorDatas(result);

...............................................

.....................................

}

问题:“第二个WCF通信方法” 依赖于 “第一个WCF通信方法”应该怎么办??? 现在是将第二个方法,写在第一个方法的最后的。

[解决办法]
写在 GetAreaHeadCountCompleted 中
[解决办法]
用一个标记变量,初值0

在GetMonitoringDatasEntityTwoCompleted 执行时判断

是否等于0 等于0赋值为2

不等于0执行要执行的方法,并重置为0

在GetMonitoringDatasEntityCompleted 执行时也判断

是否等于0 等于0赋值为3

不等于0执行要执行的方法,并重置为0

[解决办法]
把 GetMonitoringDatasEntityTwoAsync 的调用挪到 client_GetMonitoringDatasEntityCompleted 里,
把 BindChart 挪到 client_GetMonitoringDatasEntityTwoCompleted 里。

类似如下:

方法1_Async

方法1_Completed
方法2_Async

方法2_Completed
BindChart
[解决办法]

C# code
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;      }  } 

读书人网 >C#

热点推荐