How do you wait / join a WCF web service called Silverlight?

If you call a web service from Silverlight as follows:

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?

}

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

Thanks Jeff

0
source share
5 answers

To do this, you must use ManualResetEvent in your class (class level variable), and then wait for it.

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

and in the code of the event handler

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

, . . GetDataCompleted mainthreed. WaitOne .

+4

; ? , , - . , , Silverlight. , .

+1

, :

serviceClient.GetDataCompleted += (s,e) =>
{
  // Your code here
};
serviceClient.GetDataAsync();
0

, WCF, BeginX/EndX .

public class ServiceFooCoordinator : CoordinatorBase<IServiceFoo>
{
    public IAsyncResult BeginMethodFoo ()
    {
        IAsyncResult ar = null;
        IServiceFoo channel = null;
        channel = _factory.GetChannel();

        Begin( channel, () => ar = channel.BeginMethodFoo( null, channel ) );
        return ar;
    }

    public Bar[] EndMethodFoo ( IAsyncResult ar )
    {
        IServiceFoo channel = null;
        channel = _factory.GetChannel();
        return channel.EndMethodFoo( ar );
    }
}

:

 ServiceFooCoordinator _coordinator;

 var asyncResult = _coordinator.BeginMethodFoo();
 try
 {
   var result = _coordinator.EndMethodFoo( asyncResult );
 }
 catch ( Exception )
 { }

.

0

Source: https://habr.com/ru/post/1703551/


All Articles