I want to access a webservice:getMonitorData() , on creationcomplete and returns an array, in an infinite loop so that the getIndex0.text is updated each time.
Flex is not able to handle an infinite loop and gives a timeout error 1502. If I run the for loop until i<2000 or so it works fine.
How can replace the loop so that my webservice is accessed continiously and the result is shown in getIndex0.text.
This is how my application looks like:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"
xmlns:plcservicebean="server.services.plcservicebean.*"
creationComplete="clientMonitor1()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.CallResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable] public var dbl0:Number;
//-----------Infinite Loop, Works fine if condition = i<2000------------------------
public function clientMonitor1():void{
for(var i:int = 0; ; i++){
clientMonitor();
}
}
public function clientMonitor():void{
var callResp:CallResponder = new CallResponder();
callResp.addEventListener(ResultEvent.RESULT, monitorResult);
callResp.addEventListener(FaultEvent.FAULT, monitorFault);
callResp.token = plcServiceBean.getMonitorData();
}
public function monitorResult(event:ResultEvent):void{
var arr:ArrayCollection = event.result as ArrayCollection;
dbl0 = arr[0].value as Number;
}
protected function monitorFault(event:FaultEvent):void{
Alert.show(event.fault.faultString, "Error while monitoring Data ");
}
]]>
</fx:Script>
<fx:Declarations>
<plcservicebean:PlcServiceBean id = "plcServiceBean"
showBusyCursor="true"
fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" />
</fx:Declarations>
<mx:Form x="52" y="97"
label="Double">
<mx:FormItem label = "getMonitorValue">
<s:TextInput id = "getIndex0"
text = "{dbl0}"/>
</mx:FormItem>
</mx:Form>
</s:Group>
Your problem is that your loop runs synchronously while the actual web service calls are asynchronous. Flex uses a frame-based execution model and only has a single thread of execution — when you make a call to the webservice, it initiates the connection and returns immediately. Looping forever, then, means that you’ll never actually get to the next frame, where the result could be processed. Looping 2000 times means you spawn 2000 connections straight away, which will be queued up as you’re not allowed to make that many connections at once. They will then complete over the next few minutes.
The way to do what you want to do is probably to wait until one request is complete before firing the next. To achieve this, you can call
clientMonitor()from the end of your event handlers, or set up an extra event handler specifically to callclientMonitor()when the request completes.