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

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

问题描述

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

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天全站免登陆