Select to view content in your preferred language

Asynchronously adding graphics

1755
5
Jump to solution
04-01-2014 10:58 PM
Labels (1)
MichalKowalczuk
Deactivated User
Hi,

I am new to ArcGIS Runtime SDK for WPF. I want to add a lot of polygons to graphics layer. Is there possibility to do it asynchronously? Each polygon require some time to generate it and I don't want to freeze my map. My idea is user continue navigating map while new polygons are adding in background.

Any help is much appreciated. Thanks!
0 Kudos
1 Solution

Accepted Solutions
MichaelBranscomb
Esri Frequent Contributor
Hi,

You can add them individually. I was just trying to demonstrate the recommended approach which is to build a generic collection of graphics and call AddRange because it is several times faster.

In your case it may make sense to add them individually.

Cheers

View solution in original post

0 Kudos
5 Replies
MichaelBranscomb
Esri Frequent Contributor
Hi,

You could use the .NET Task framework to achieve this. In the example below the call to AddGraphicsToGraphicsLayer() is not awaited therefore the button click logic will continue, displaying the MessageBox declaring "Done". Meanwhile the async Task continues to run adding graphics to the graphics layer which is populated a short time later (I added an artificial Delay).

private void Button_Click(object sender, RoutedEventArgs e)
{
 try
 {
  var _ =  AddGraphicsToGraphicsLayer();
 }
 catch (System.Exception)
 {
  MessageBox.Show("Error");
 }
 MessageBox.Show("Done");
}

private async Task AddGraphicsToGraphicsLayer() 
{
 List<Graphic> graphics = new List<Graphic>();

 for (int i = 0; i < 100; i++)
 {
  Graphic g = new Graphic() 
  {
    Geometry = GetRandomPolygon(),
  };

  graphics.Add(g);
  await Task.Delay(50);
 }

 MyGraphicsLayer.Graphics.AddRange(graphics);
}

Random random = new Random();

private Polygon GetRandomPolygon() 
{
 Polygon polygon = new Polygon();
 ESRI.ArcGIS.Client.Geometry.PointCollection pointCollection = new ESRI.ArcGIS.Client.Geometry.PointCollection();

 MapPoint mapPoint = new MapPoint(random.Next(-20000000, 20000000), random.Next(-20000000, 20000000));
 MapPoint mapPoint2 = new MapPoint(random.Next(-20000000, 20000000), random.Next(-20000000, 20000000));
 MapPoint mapPoint3 = new MapPoint(random.Next(-20000000, 20000000), random.Next(-20000000, 20000000));

 pointCollection.Add(mapPoint);
 pointCollection.Add(mapPoint2);
 pointCollection.Add(mapPoint3);

 polygon.Rings.Add(pointCollection);

 return polygon;
}


Cheers

Mike
0 Kudos
AnttiKajanus1
Deactivated User
One thing to highlight from snipped from Mike is that remember to use AddRange(graphics) instead of Add(graphic) since that performs better.

Also use Renderers instead of individual symbols.
0 Kudos
MichalKowalczuk
Deactivated User

 for (int i = 0; i < 100; i++)
 {
  Graphic g = new Graphic() 
  {
    Geometry = GetRandomPolygon(),
  };

  graphics.Add(g);
  await Task.Delay(50);
 }

 MyGraphicsLayer.Graphics.AddRange(graphics);



Mike,
I must confess that asynchronous programming is something new to me but if I properly understand in your code you add all polygons to graphics layer after for-loop.
My idea was to add each polygon to layer inside loop (using Add method instead of AddRange), because generating polygons takes critical part of time and I want to see results consecutively on the map. Is it possible?

Or maybe I misunderstood you sample;-)

Cheers!
Mike
0 Kudos
MichaelBranscomb
Esri Frequent Contributor
Hi,

You can add them individually. I was just trying to demonstrate the recommended approach which is to build a generic collection of graphics and call AddRange because it is several times faster.

In your case it may make sense to add them individually.

Cheers
0 Kudos
MichalKowalczuk
Deactivated User
After little change in code above I get what I want. Thanks!


private async Task AddGraphicsToGraphicsLayer() 
{
 for (int i = 0; i < 100; i++)
 {
  // time-consuming operation
  await Task.Delay(50);

  Graphic g = new Graphic() 
  {
    Geometry = GetRandomPolygon(),
  };

  MyGraphicsLayer.Graphics.Add(g);
 }
}

0 Kudos