2011-03-11 66 views
0

我正在使用Silverlight 4和WCF服务进行数据库交互。我面临着一个问题。使用WCF方法异步调用结果

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; 
    } 

但所有方法银光调用是异步,所以我不能够得到这一点。

+0

我也发现了同样的在上面这个链接http://www.codeproject.com/KB/silverlight/SyncCallInSilverlight.aspx?msg=3571404但使用的JavaScript的我的应用程序运行时OOB – JSJ 2011-03-11 13:26:34

回答

1

把你想要做的事情放到另一个方法中,并从你的Completed Handler中调用该方法。

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

    MyNewMethod(Name); 
} 
+0

感谢这个但我想在接到电话后工作。 – JSJ 2011-03-11 13:24:01

+0

也许我失去了一些东西。在您想要的地方拨打电话而不是在完成的处理程序中进行拨打的优点是什么? – Geoff 2011-03-11 13:45:04

+0

我是新来的wcf你能告诉我如何调用非async方法 – JSJ 2011-03-11 14:02:20