在Flex中编程同步Web服务调用 [英] Programming synchronous web service calls in flex

查看:74
本文介绍了在Flex中编程同步Web服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web服务调用在flex中是异步的,但是我想将Web服务调用包装在一个类中以提供同步封装.类似于以下内容-用户可以调用getMyMethodResult并获取Web服务返回的结果.我希望接收到肥皂响应的线程将填充变量_result,并且意味着一段时间后,getMyMethod将发现_result不再为null.但事实并非如此!谁能解释为什么这行不通?

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

推荐答案

绝对没有对此的支持.事件循环在帧之间运行,只要您用(无限)循环阻止执行,就不会调用myMethodListener函数.无论如何,这将是一个糟糕的主意,因为Flash Player中缺少线程会导致您的UI在等待服务返回时冻结.您应该放弃这个想法.

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.

这篇关于在Flex中编程同步Web服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆