Select to view content in your preferred language

Using Geometry service using TPL

707
2
10-04-2012 10:40 AM
George_J_Capnias
Emerging Contributor
Hi,
I am trying to implement a sequence of calls to Geometry service. I want to implements these calls as Tasks.
I have the following extension method,
public static Task<IList<Graphic>> SimplifyTask(this GeometryService gc, IList<Graphic> graphics)
{
 TaskCompletionSource<IList<Graphic>> tcs = new TaskCompletionSource<IList<Graphic>>();
 EventHandler<GraphicsEventArgs> SimplifyCompleted = delegate { };
 EventHandler<TaskFailedEventArgs> Failed = delegate { };
 SimplifyCompleted = (sender, e) =>
 {
  gc.SimplifyCompleted -= SimplifyCompleted;
  gc.Failed -= Failed;
  tcs.TrySetResult(e.Results);
 };
 Failed = (sender, e) =>
 {
  gc.SimplifyCompleted -= SimplifyCompleted;
  gc.Failed -= Failed;
  tcs.TrySetException(e.Error);
 };
 try
 {
  gc.SimplifyCompleted += SimplifyCompleted;
  gc.Failed += Failed;
  gc.SimplifyAsync(graphics);
 }
 catch (Exception ex)
 {
  tcs.TrySetException(ex);
  gc.SimplifyCompleted -= SimplifyCompleted;
  gc.Failed -= Failed;
 }
 return tcs.Task;
}

       
that I am trying to use through this code:
 geometryService = new GeometryService("http://arcgis/ArcGIS/rest/services/Geometry/GeometryServer");
 Task<IList<Graphic>> task1 = geometryService.SimplifyTask(PstLayer.Graphics);
 Task.WaitAll(task1);
 IList<Graphic> simplifiedPstLayer = task1.Result;
When the code reaches "Task.WaitAll(task1);" line, I wait for ever; Having breakpoints inside SimplifyCompleted and Failed handlers, the execution never reaches this point...
Is there any help, pointers somebody has to offer?

Thanks,
George J.
0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
 Task.WaitAll(task1);

When the code reaches "Task.WaitAll(task1);" line, I wait for ever;


Your call to WaitAll is blocking the UIThread and as SimplifyAsync is an asynchronous method which needs the UIThread, you are stuck.

It's exactly what the new C# 'await' operator is done for : i.e. to use asynchonous task without blocking the thread.

That being said, .Net Framework 4.0 and SL5 doesn't support the 'await' operator except if you download and install the Async Targetting Pack

Without this pack, you can get the same behavior by using the ContinueWith method.

Something like:

Task<IList<Graphic>> task1 = geometryService.SimplifyTask(PstLayer.Graphics);
task1.ContinueWith(t =>
{
    IList<Graphic> simplifiedPstLayer = t.Result;

    // add here your code using the simplified graphics

}, TaskScheduler.FromCurrentSynchronizationContext()); 


// No more code here


Obviously the syntaxic sugar of the await operator gets lost.

Note : another option would be to execute your task in another thread, but in this particular case it's not working since the graphics need to be created in the UIThread and so SymplifyAsync can only be executed in the UI Thread.
0 Kudos
DenisT
by
Deactivated User
Better late than never.

There is an extension for SL API that can help you with async/await:
https://arcgisasync.codeplex.com/
0 Kudos