tags:

views:

378

answers:

1

Web service calls are asynchronous in flex, but I wanted to wrap a web service call in a class to provide synchronous encapsulation. Something like the below - the user could call getMyMethodResult and get the result returned by the web service. I expected the thread that recieved the soap response would populate the variable _result and mean that getMyMethod would, after a time, find _result is not longer null. But it doesn't! Can anyone explain why this does not work?

public class myClass { private var _result:Object;

public function myClass()
{
 //create a web service object
 ...

 // Add listener
 _service.addMyMethodListener(myMethodListener);
}

public function getMyMethodResult()
{
 _service.myMethod();

 while (_result == null)
 {
  // count a variable or something (unimportant)
 }

 return _result;
}

private function myMethodListener(event:Event):void
{
 _result = event.result;
}

}

+1  A: 

There's is absolutely no support for that. The event loop runs between frames and as long as you block the execution with your (infinite) loop, your myMethodListener function will not be called. Anyway, this would be a terrible idea since the absence of threading in the Flash Player will cause your UI to freeze while you wait for your service to return. You should just drop that idea.

sharvey
Thanks for your reply. I still don't understand; I thought they were running in different threads? How is the flash player handling the asynchronous return if not by a different thread? If the event loop is handling everything, could you explain the execution order of these tasks? Thanks
El Moz
BTW, I am happy to "drop the idea", I was just wondering why it didn't work, so I could better understand the way the flash player/ flex operated. I am new to flex!
El Moz
The flash player wont interrupt your code execution when it receives an event like that. It's going to wait until the next frame , if I'm not mistaken. The background implementation of the player is probably using a thread to do that, but queues the events so that the actionscript is executed on one thread only.
sharvey