您如何等待/加入从 Silverlight 调用的 WCF Web 服务? [英] How do you wait/join on a WCF Web Service called from Silverlight?

查看:27
本文介绍了您如何等待/加入从 Silverlight 调用的 WCF Web 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您像这样从 Silverlight 调用 Web 服务:

If you call a web service from Silverlight like this:

MyServiceClient serviceClient = new MyServiceClient();
void MyMethod()
{
  serviceClient.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(serviceClient_GetDataCompleted);
  serviceClient.GetDataAsync();

  // HOW DO I WAIT/JOIN HERE ON THE ASYNC CALL, RATHER THAN BEING FORCE TO LEAVE THIS METHOD?

}

我宁愿等待/加入MyMethod"中的异步服务线程,也不愿在调用GetDataAsync"后离开MyMethod",最好的方法是什么?

I would rather wait/join with the asych service thread inside "MyMethod" rather than leaving "MyMethod" after calling "GetDataAsync", what is the best way to do this?

谢谢,杰夫

推荐答案

为此,您需要使用 ManualResetEvent 在您的类(类级别变量)中,然后等待它.

To do this you would use a ManualResetEvent in your class (class level variable) and then wait on it.

void MyMethod()
{
  wait = new ManualResetEvent(false);
  // call your service
  wait.WaitOne();
  // finish working
}

并在您的事件处理程序代码中

and in your event handler code

void serviceClient_GetDataCompleted(...)
{
  // Set values you need from service
  wait.Set();
}

这篇关于您如何等待/加入从 Silverlight 调用的 WCF Web 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆