2011-03-11 63 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