如何检测等待的异步 wcf 调用?
我从 silverlight 应用程序调用 WCF 服务。 我正在异步执行此操作,并且在进行异步后不会阻止执行。 调用(这意味着,我没有使用等待加入机制 frm 此页面)。 我不希望流量被阻塞。
但是,我想检测 WCF 调用已进入等待状态,以便我可以在 UI 上显示忙碌图标 - 一种视觉通信,表明 UI 后面正在发生事情。
我可以更改我的代码,以便可以开始为忙碌图标设置动画,并在异步调用完成时停止该动画。
然而,这是大量的样板代码,并且随着整个客户端代码中进行更多的调用,这只会变得更加混乱。
那么,wcf 服务客户端参考代码是否公开了任何方法或属性,可用于在任何异步 wcf 服务调用进入等待状态时触发关闭事件,同样,在所有异步 wcf 服务调用完成时触发关闭事件?
I make calls to a WCF service from my silverlight application.
I am doing this asynchronously and I am not blocking execution after making the async. call (that means, I am not using wait-join mechanism frm this page). I do not want the flow to get blocked.
However, I would like to detect that the WCF call has gone into a wait state so that I can show a busy icon on the UI - a visual communication indicating that things are happening behind the UI.
I can change my code such that I can start to animate the busy icon and stop that animation when the asynchronous call completes.
However, this is a lot of bolierplate code, and with more calls being made throughout the client code, this is only going to get messier.
So, is there any method or property exposed by the wcf service client reference code that can be used to trigger off events when any async wcf service calls go into a wait state, and likewise, trigger off events when the all async wcf service calls finish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
生成的客户端引用类上没有任何属性或事件可用于标识对 Silverlight WCF 服务方法的异步调用当前正在进行。 不过,您可以使用一个简单的布尔变量自己记录这一点,或者使用您提到的在这种情况下要避免的阻塞线程同步。
以下是如何使用 Silverlight ProgressBar 控件 指示正在等待/正在处理对非常简单的 Silverlight Web 服务的调用:
Page.xaml:
Page.xaml.cs:
在异步调用 DoWork 后将 IsInminateate 设置为 true使进度条像这样不确定地动画:
alt text http://www.freeimagehosting.net/uploads /89620987f0.png
因为 OnClientDoWorkCompleted 的回调发生在 UI 线程上,所以可以在方法主体中将 IsInminated 属性的值更改回 false; 当工作/等待现已完成时,这会再次导致无动画的空白进度条。
下面是 Web 服务的代码以及上面代码异步调用的 DoWork 方法,它所做的一切都是通过休眠 5 秒来模拟一些长时间运行的任务:
There is no property or event on the generated client reference class that can be used to identify that an asynchronous call to a method of a Silverlight WCF service is currently in progress. You can record this yourself using a simple boolean variable though, or by using the blocking thread synchronization that you mentioned you want to avoid in this case.
Here's an example of how to do what you want using the Silverlight ProgressBar control to indicate waiting/working on a call to a very simple Silverlight web service:
Page.xaml:
Page.xaml.cs:
Setting IsIndeterminate to true after the asynchronous call to DoWork makes the progress bar animate indeterminately like this:
alt text http://www.freeimagehosting.net/uploads/89620987f0.png
Because the callback to OnClientDoWorkCompleted happens on the UI thread it's fine to change the value of the IsIndeterminate property back to false in the method body; this results in a non-animating blank ProgressBar again as the working/waiting is now finished.
Below is the code for the web service and the DoWork method that the above code calls asynchronously, all it does it simulate some long running task by sleeping for 5 seconds: