使用WCF方法异步调用结果

时间:2011-03-11 12:57:12

标签: wcf silverlight-4.0

我正在使用带有WCF服务的Silverlight 4进行数据库交互。我面临一个问题。

所有在Silverlight应用程序中的功能。

    ServiceReference1.WCFSLServicesClient wc = new ServiceReference1.WCFSLServicesClient();

    private void button1_Click(object sender, RoutedEventArgs e)
    {
       _wait = new ManualResetEvent(false);
       wc.SayHelloCompleted += new EventHandler<ServiceReference1.SayHelloCompletedEventArgs>(wc_SayHelloCompleted);

       wc.SayHelloAsync("Mr. X");
//wait untill the call finish and then move next like     

       //Here I want to do some thing with result of above call.  And then proceed to next task . 

    }

    String Name = String.Empty;
    void wc_SayHelloCompleted(object sender, ServiceReference1.SayHelloCompletedEventArgs e)
    {
       Name =e.Result;
    }

但Silver Light中的所有方法调用都是Async,所以我无法解决这个问题。

1 个答案:

答案 0 :(得分:1)

将您想要做的任何内容放入另一个方法中,并从Completed Handler中调用该方法。

void wc_SayHelloCompleted(object sender, ServiceReference1.SayHelloCompletedEventArgs e)
{
   Name =e.Result;

   MyNewMethod(Name);
}