CancelAsync in GeometryService

578
2
Jump to solution
02-25-2014 11:45 AM
LuisGarcia2
Occasional Contributor II
I am trying to project asynchronously a number of graphics and I am having an issue with a busy GeometryService. Our application has a set of control that filters the data (Graphics) the user wants to see displayed on the map. Some controls (or filters as we called them) are just a Yes/no control (a flag) and others are a slider to narrow/expand the data. The flag controls work just fine because when the client selects/unselects the event is just one and whatever data is needed to be projected is aggregated in a List and sent asynchronously like this:

myGeomSer.ProjectAsync(myListOfPoints, mapControl1.map1.SpatialReference);  


The problem is with the slider. When my client uses the slider to narrow the data then the event that the slider is moving is firing every single time the slider is moved. The events are happening faster that the time it takes the GeometryService to come back with the results and it fires an exception when I try to do another ProjectAsync while it is busy. What I did was to test whether the service is busy and the cancel whatever is doing and request a new ProjectAsync hoping that the last one would contain the results I was expecting. The code looks like this:

                       if (myGeomSer.IsBusy)                        {                            myGeomSer.CancelAsync();                            myGeomSer.ProjectAsync(myListOfPoints, mapControl1.map1.SpatialReference);                         }                        else                        {                            myGeomSer.ProjectAsync(myListOfPoints, mapControl1.map1.SpatialReference);                         }


That looks all good except that the service never comes back to  the method:

myGeomSer_ProjectCompleted(object sender, ESRI.ArcGIS.Client.Tasks.GraphicsEventArgs e)


So my question, is there a way to do this operation? how can I cancel the ProjectAsync and launch it again so I can get the results I need when the client has stopped moving the slider? I don't have access to the slider events because I'm integrating the map in a framework and we get the events based on changes on the data and not the GUI's.

Thanks!
0 Kudos
1 Solution

Accepted Solutions
AhmedEl-Sisi
Occasional Contributor III
You can initialize a new instance form GeometryServic each time you call it.

Also you can send userToken with your request so you can make sure you display the latest request only.

        GeometryService myGeomSer;         string currentUserToken;          private void ProjectGraphics(List<Graphic> myListOfPoints)         {             if (myGeomSer != null && myGeomSer.IsBusy)             {                 myGeomSer.CancelAsync();             }             myGeomSer = new GeometryService(serviceURL);              myGeomSer.ProjectCompleted += geometryService_ProjectCompleted;             myGeomSer.Failed += GeometryService_Failed;             currentUserToken = Convert.ToString(DateTime.Now.Ticks); //use any unique value like GUID             myGeomSer.ProjectAsync(myListOfPoints, mapControl1.map1.SpatialReference, currentUserToken);         }          private void geometryService_ProjectCompleted(object sender, GraphicsEventArgs e)         {             if (Convert.ToString( e.UserState)==currentUserToken)             {                 //             }         }

View solution in original post

0 Kudos
2 Replies
AhmedEl-Sisi
Occasional Contributor III
You can initialize a new instance form GeometryServic each time you call it.

Also you can send userToken with your request so you can make sure you display the latest request only.

        GeometryService myGeomSer;         string currentUserToken;          private void ProjectGraphics(List<Graphic> myListOfPoints)         {             if (myGeomSer != null && myGeomSer.IsBusy)             {                 myGeomSer.CancelAsync();             }             myGeomSer = new GeometryService(serviceURL);              myGeomSer.ProjectCompleted += geometryService_ProjectCompleted;             myGeomSer.Failed += GeometryService_Failed;             currentUserToken = Convert.ToString(DateTime.Now.Ticks); //use any unique value like GUID             myGeomSer.ProjectAsync(myListOfPoints, mapControl1.map1.SpatialReference, currentUserToken);         }          private void geometryService_ProjectCompleted(object sender, GraphicsEventArgs e)         {             if (Convert.ToString( e.UserState)==currentUserToken)             {                 //             }         }
0 Kudos
LuisGarcia2
Occasional Contributor II
Ahmed, thanks! That works well.
0 Kudos