积极答复者
“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通信方法”应该怎么办??? 现在是将第二个方法,写在第一个方法的最后的。
第二种情况:
void client_GetMonitoringDatasEntityTwoCompleted(object sender, GetMonitoringDatasEntityTwoCompletedEventArgs e)
{
monitoringstwo = e.Result;
}
void client_GetMonitoringDatasEntityCompleted(object sender, GetMonitoringDatasEntityCompletedEventArgs e)
{
monitoringsone = e.Result;
}
private void BindChart()
{
List<Monitoring> noemwmgm = new List<Monitoring>();
IEnumerable<KeyValuePair<KeyValuePair<string, string>, double[]>> datas;
if (monitoringsone.Count > 0 && monitoringstwo.Count > 0)
{问题二:“monitoringsone” 和 “monitoringstwo” 是异步通信返回的结果集,两者没有执行完返回,这里就会报错。
如何判断“GetMonitoringDatasEntityCompleted”和“GetMonitoringDatasEntityTwoCompleted”的执行情况???如果都执行完了,就执行下面的方法。否则等待没有执行完“通信方法”执行完,都执行完了才执行下面的方法。 应该怎么做???
}
}
完整的代码:
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);
BindChart();
}
void client_GetMonitoringDatasEntityTwoCompleted(object sender, GetMonitoringDatasEntityTwoCompletedEventArgs e)
{
monitoringstwo = e.Result;
}
void client_GetMonitoringDatasEntityCompleted(object sender, GetMonitoringDatasEntityCompletedEventArgs e)
{
monitoringsone = e.Result;
}
private void BindChart()
{
List<Monitoring> noemwmgm = new List<Monitoring>();
IEnumerable<KeyValuePair<KeyValuePair<string, string>, double[]>> datas;
if (monitoringsone.Count > 0 && monitoringstwo.Count > 0)
{
问题:关键是对“GetMonitoringDatasEntityTwoCompleted” 和“GetMonitoringDatasEntityCompleted”判断。怎么判断能写写吗???谢谢了,Tanks a lot...
}
Science and technology is my lover.
- 已编辑 starrycheng 2012年6月1日 3:16
答案
-
我感觉你这样设计不好,在异步系统中,你最好不要采用线程依赖的关系,因在在异步调用中,都会在后台新建一个线程去执行程序,但是你并知道这个线程什么时候执行完。
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
- 已编辑 Peter pi - MSFTModerator 2012年6月4日 6:13
- 已标记为答案 starrycheng 2012年8月6日 1:37
-
我感觉你这样设计不好,在异步系统中,你最好不要采用线程依赖的关系,因在在异步调用中,都会在后台新建一个线程去执行程序,但是你并知道这个线程什么时候执行完。
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
补充:)
我对WCF没有什么研究,不过说到线程,建议:定义一个委托Action,然后使用Action的BeginInvoke,当调用BeginInvoke的时候是内部开辟了一个隐式的后台线程处理,当Action里边复杂的事情执行完毕之后才执行BeginInvoke中的内容(可以参考:使用委托的BeginInvoke方法来完成复杂任务的操作)。
- 已标记为答案 starrycheng 2012年8月6日 1:37
-
你用在SilverLight中的,就没办法回调了,我说的回调是指服务回调,非方法回调。
你整理一下你的思路吧,我的主贴我看的有点晕,我建议你修改设计。
比如你在第一次异步请求完成的回调里发起第二次的异步请求,异步操作太多增加了代码的维护难度。
快乐在于能够长时间的为自己认为值得的事情努力工作,不管它是什么。
- 已标记为答案 starrycheng 2012年8月6日 1:37
全部回复
-
我感觉你这样设计不好,在异步系统中,你最好不要采用线程依赖的关系,因在在异步调用中,都会在后台新建一个线程去执行程序,但是你并知道这个线程什么时候执行完。
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
- 已编辑 Peter pi - MSFTModerator 2012年6月4日 6:13
- 已标记为答案 starrycheng 2012年8月6日 1:37
-
我感觉你这样设计不好,在异步系统中,你最好不要采用线程依赖的关系,因在在异步调用中,都会在后台新建一个线程去执行程序,但是你并知道这个线程什么时候执行完。
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
补充:)
我对WCF没有什么研究,不过说到线程,建议:定义一个委托Action,然后使用Action的BeginInvoke,当调用BeginInvoke的时候是内部开辟了一个隐式的后台线程处理,当Action里边复杂的事情执行完毕之后才执行BeginInvoke中的内容(可以参考:使用委托的BeginInvoke方法来完成复杂任务的操作)。
- 已标记为答案 starrycheng 2012年8月6日 1:37
-
我感觉你这样设计不好,在异步系统中,你最好不要采用线程依赖的关系,因在在异步调用中,都会在后台新建一个线程去执行程序,但是你并知道这个线程什么时候执行完。
Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
Thanks a lot...
这个WCF是使用在“Silverlight”中的,一个WCF就是一个新线程,对吗???如果是这样问题应该怎样解决???
Science and technology is my lover.
-
你用在SilverLight中的,就没办法回调了,我说的回调是指服务回调,非方法回调。
你整理一下你的思路吧,我的主贴我看的有点晕,我建议你修改设计。
比如你在第一次异步请求完成的回调里发起第二次的异步请求,异步操作太多增加了代码的维护难度。
快乐在于能够长时间的为自己认为值得的事情努力工作,不管它是什么。
- 已标记为答案 starrycheng 2012年8月6日 1:37