views:

36

answers:

2

Below is the code i used to consume web servcie in SilverLight.

private void button1_Click(object sender, RoutedEventArgs e)
{
      BasicHttpBinding bind = new BasicHttpBinding();
      EndpointAddress endpoint = new EndpointAddress("http://loalhost/Service.asmx");
      ServiceSoapClient client = new ServiceSoapClient(bind, endpoint);
      client.RunHelloCompleted += new EventHandler<RunHelloCompletedEventArgs>(client_RunQwinCompleted);
      client.RunHelloAsync(command);
 }

 void client_RunHelloCompleted(object sender, RunHelloCompletedEventArgs e)
 {
      txtProcess.Text=Process(e.Result);
 }

I want to know a way that after i run RunHelloAsync(Command), I want to get the returned result without going to Completed event. Please advise me. thank you.

+1  A: 

Simple answer : You can't. Everything in Silverlight is Asynchronous so there is no way to block after the client.RunHelloAsync(command) call and wait for the result.

Long answer : There are ways to simulate working with calls in a synchronous fashion, but the calls still being made asynchronously. Take a look at this thread for a few more answers.

Stephan
A: 

Hi,thanks for reply. I used the method below,It's working for me. But I have to use wcf service instead of web service.But in this program , using wcf or web service wont have any effect. So, I decided to use wcf.

            Service s = new ServiceClient() as Service;

            s.BeginHello(command, new AsyncCallback((result) =>
                {

                    string res = s.EndHello(result);

                    Dispatcher.BeginInvoke(
                  () =>
                  {
                     //process answer here.
                  }
                  );
                }
                ), null);

i found this from below link : http://wildermuth.com/2008/12/26/Using_Closures_to_Simplify_Asynchronous_Programming

David