5 Replies Latest reply on Jun 22, 2010 1:55 PM by _H_P_

    Replace infinite loop

    _H_P_ Level 1

      Hello,

       

      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 for i<2000 or so, it works fine.

       

      How can replace the infinite 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>
      
        • 1. Re: Replace infinite loop
          Flex harUI Adobe Employee

           

                              clientMonitor();

                      }

                     

                      protected function monitorFault(event:FaultEvent):void{

                          Alert.show(event.fault.faultString, "Error while monitoring

          Data ");        

                              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;

           

          • 2. Re: Replace infinite loop
            David_F57 Level 5

            hi,

             

            having loops that never end are basically uncontrollable, first the cpu is going to get thrashed, second you are calling a service over and over again before there is any gaurantee its even returned, why not just recall the service in the return handler. Better yet look at polling.

             

            David.

            • 3. Re: Replace infinite loop
              _H_P_ Level 1

              Hello David,

               

              Thank you for your post.

               

              Like you suggested, this is what I have. I am calling my web service on creationcomplete(). In the result handler I am again calling my web service, once I receive a result. Can you advise if this would be a good way, given that I have to call my web service continuously. Is there a better solution.

               

              I am calling my monitorResult(event) at the time of declaration of my service and through callResponder. What is the difference between the two. The reason I am using a callresponder is so that I can reuse the callresponder else where in my application to call the same web service.

               

              Thank you again.


              H

               

              <?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= "clientMonitor()">
                  <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;
                          public var callResp:CallResponder;
                          
                          public function clientMonitor():void{
                              callResp = 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;
                              plcServiceBean.getMonitorData();
                          }
                          
                          protected function monitorFault(event:FaultEvent):void{
                              Alert.show(event.fault.faultString, "Error while monitoring Data ");
                          }
                      ]]>
                  </fx:Script>
                  <fx:Declarations>
                      <plcservicebean:PlcServiceBean id = "plcServiceBean" 
                                                     result = "monitorResult(event)"
                                                     fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" />
                      <!--result= "handleMonitor(event)"-->
                      
                  </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>
              
              • 4. Re: Replace infinite loop
                David_F57 Level 5

                hi,

                 

                first thing is you can remove 1 or 2 things(just tidier thats all), Rather than creating you responder in the createComplete function use the  declaration block, now all you need is the call in creation complete function. Remove the call to monitorResult in the service its not required..

                 

                declarations -----

                 

                <plcservicebean:PlcServiceBean id = "plcServiceBean" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" />

                <s:CallResponder id="MonitorResult" result="MonitorResult _resultHandler(event)"/>

                 

                 

                 

                creationcomplete ---

                 

                MonitorResult.token = plcServiceBean.getMonitorData();

                1 person found this helpful
                • 5. Re: Replace infinite loop
                  _H_P_ Level 1

                  Hello David,

                   

                  Thank you again.

                   

                  I modified my code according to your suggestion. However it works if i add

                   result = "monitorResult_resultHandler(event)"

                   

                  to my webservice declaration and not to the callResponder. Will I still be able to reuse my CallResponder

                   

                  <fx:Declarations>
                       <plcservicebean:PlcServiceBean id = "plcServiceBean" 
                                                         result = "monitorResult_resultHandler(event)"
                                                         fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" />
                          
                       <s:CallResponder id="monitorResult" />
                                           <!-- result= "monitorResult_resultHandler(event)" -->
                  </fx:Declarations>
                  

                  Thanks,

                   

                  H