Select to view content in your preferred language

Canceling an Asynchronous call

788
4
12-10-2010 06:15 AM
CoreySchafer
Deactivated User
Hey there, I was wondering how to cancel an asynchronous call? It displays the correct results that I'm looking for; however, when I click my cancel button halfway through the loading process, I don't want it to return anything after that. So far I only have a cancel button that clears the graphics from the map, but then they redisplay once the execution is complete. Is there a method I can use to prevent this? Thanks in advance.

if(driveTimes.length){
      var params:Object = 
       {        
        //Async params
        
        "SDESQL_SDE_inPoints_NAD83": featureSet,
        "Impedance_attribute": impedanceAttr,
        "Default_break_values": driveTimes
       };
      
      serviceAreaService.outSpatialReference = map.spatialReference;
      serviceAreaService.submitJob(params, new AsyncResponder(submitComplete, onFault), new AsyncResponder(submitStatus, onFault));
      showMessage("loading", true);
      

function submitStatus(gpStatus:JobInfo, token:Object = null):void
    {
     // showMessage(loadingLabel, true);
     showMessage("Asynchronous", true);
    }

function submitComplete(gpResult:JobInfo, token:Object = null):void
    {
     serviceAreaService.getResultData(gpResult.jobId, "serviceArea_shp", new AsyncResponder(submitResult, onFault));
    }
    
function submitResult(pv:ParameterValue, token:Object = null):void
    {
     var fs:FeatureSet = pv.value as FeatureSet;      
     
     var symRenderer:UniqueValueRenderer = new UniqueValueRenderer();
     symRenderer.attribute = "ToBreak";
     symRenderer.defaultSymbol = fillSym3;
     var symRendererInfos:Array = new Array();
     symRendererInfos.push(new UniqueValueInfo(fillSym1, numTime1.value.toString()));
     symRendererInfos.push(new UniqueValueInfo(fillSym2, numTime2.value.toString()));
     symRenderer.infos = symRendererInfos;
     graphicsLayer.renderer = symRenderer;     
     
     graphicsLayer.graphicProvider = fs.features;
     graphicsLayer.add(graphic);     
     map.extent = getFeatureSetExtent(fs);
     clearMessage();
    }
    
    function onFault(info:Object, token:Object = null):void
    {
     showMessage(info.toString(), false);
    }
    
Tags (2)
0 Kudos
4 Replies
DasaPaddock
Esri Regular Contributor
There isn't a cancel method in the API, but one approach could be to use event listeners instead of responders and then you could remove the listener and it wouldn't be called. Another approach could be to keep using responders but create a class member like private var requestCancelled:Boolean that you check in your submitComplete().
0 Kudos
CoreySchafer
Deactivated User
Thanks, that seems to work great. Appreciate the help!
0 Kudos
CoreySchafer
Deactivated User
Dasa,

False alarm. Thought I had it working, but not what I expected. I was wondering about your var approach requestCancelled:Boolean. Say I do make a variable like this and set it to true when a user clicks the "cancel request button". What would I do once this variable is set to true if there is no method to cancel the request?

As of now I have a map that is displaying a service area. When the user clicks clear, everything gets cleared away. However, a little time after, the data they requested gets displayed on the screen. I would just like for the request to be canceled and for the user to be able to start over from scratch. This would be useful if, for example, a user accidentally requests a serviceArea of 1000 minutes when they only meant to type 100 minutes. Is this possible? And again, thanks for your help.

Oh and PS, I can use a synchronous or an asynchronous call if one is easier to cancel than the other. Thanks!
0 Kudos
DasaPaddock
Esri Regular Contributor
I was thinking you could change your submitComplete to be like this:

function submitComplete(gpResult:JobInfo, token:Object = null):void
    {
if (!requestCancelled)
{
     serviceAreaService.getResultData(gpResult.jobId, "serviceArea_shp", new AsyncResponder(submitResult, onFault));
}
    }
0 Kudos