<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Asynchronously adding graphics in ArcGIS Runtime SDK for WPF (Retired) Questions</title>
    <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561886#M2853</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
private void Button_Click(object sender, RoutedEventArgs e)
{
 try
 {
&amp;nbsp; var _ =&amp;nbsp; AddGraphicsToGraphicsLayer();
 }
 catch (System.Exception)
 {
&amp;nbsp; MessageBox.Show("Error");
 }
 MessageBox.Show("Done");
}

private async Task AddGraphicsToGraphicsLayer() 
{
 List&amp;lt;Graphic&amp;gt; graphics = new List&amp;lt;Graphic&amp;gt;();

 for (int i = 0; i &amp;lt; 100; i++)
 {
&amp;nbsp; Graphic g = new Graphic() 
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; Geometry = GetRandomPolygon(),
&amp;nbsp; };

&amp;nbsp; graphics.Add(g);
&amp;nbsp; 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;
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 00:14:10 GMT</pubDate>
    <dc:creator>MichaelBranscomb</dc:creator>
    <dc:date>2021-12-12T00:14:10Z</dc:date>
    <item>
      <title>Asynchronously adding graphics</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561885#M2852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help is much appreciated. Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Apr 2014 05:58:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561885#M2852</guid>
      <dc:creator>MichalKowalczuk</dc:creator>
      <dc:date>2014-04-02T05:58:53Z</dc:date>
    </item>
    <item>
      <title>Re: Asynchronously adding graphics</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561886#M2853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
private void Button_Click(object sender, RoutedEventArgs e)
{
 try
 {
&amp;nbsp; var _ =&amp;nbsp; AddGraphicsToGraphicsLayer();
 }
 catch (System.Exception)
 {
&amp;nbsp; MessageBox.Show("Error");
 }
 MessageBox.Show("Done");
}

private async Task AddGraphicsToGraphicsLayer() 
{
 List&amp;lt;Graphic&amp;gt; graphics = new List&amp;lt;Graphic&amp;gt;();

 for (int i = 0; i &amp;lt; 100; i++)
 {
&amp;nbsp; Graphic g = new Graphic() 
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; Geometry = GetRandomPolygon(),
&amp;nbsp; };

&amp;nbsp; graphics.Add(g);
&amp;nbsp; 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;
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:14:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561886#M2853</guid>
      <dc:creator>MichaelBranscomb</dc:creator>
      <dc:date>2021-12-12T00:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: Asynchronously adding graphics</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561887#M2854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;One thing to highlight from snipped from Mike is that remember to use AddRange(graphics) instead of Add(graphic) since that performs better.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also use Renderers instead of individual symbols.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Apr 2014 10:18:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561887#M2854</guid>
      <dc:creator>AnttiKajanus1</dc:creator>
      <dc:date>2014-04-02T10:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Asynchronously adding graphics</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561888#M2855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
 for (int i = 0; i &amp;lt; 100; i++)
 {
&amp;nbsp; Graphic g = new Graphic() 
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; Geometry = GetRandomPolygon(),
&amp;nbsp; };

&amp;nbsp; graphics.Add(g);
&amp;nbsp; await Task.Delay(50);
 }

 MyGraphicsLayer.Graphics.AddRange(graphics);
&lt;/PRE&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Or maybe I misunderstood you sample;-)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:14:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561888#M2855</guid>
      <dc:creator>MichalKowalczuk</dc:creator>
      <dc:date>2021-12-12T00:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: Asynchronously adding graphics</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561889#M2856</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In your case it may make sense to add them individually.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 02 Apr 2014 12:14:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561889#M2856</guid>
      <dc:creator>MichaelBranscomb</dc:creator>
      <dc:date>2014-04-02T12:14:43Z</dc:date>
    </item>
    <item>
      <title>Re: Asynchronously adding graphics</title>
      <link>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561890#M2857</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;After little change in code above I get what I want. Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
private async Task AddGraphicsToGraphicsLayer() 
{
 for (int i = 0; i &amp;lt; 100; i++)
 {
&amp;nbsp; // time-consuming operation
&amp;nbsp; await Task.Delay(50);

&amp;nbsp; Graphic g = new Graphic() 
&amp;nbsp; {
&amp;nbsp;&amp;nbsp;&amp;nbsp; Geometry = GetRandomPolygon(),
&amp;nbsp; };

&amp;nbsp; MyGraphicsLayer.Graphics.Add(g);
 }
}
&lt;/PRE&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 00:14:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-runtime-sdk-for-wpf-retired-questions/asynchronously-adding-graphics/m-p/561890#M2857</guid>
      <dc:creator>MichalKowalczuk</dc:creator>
      <dc:date>2021-12-12T00:14:15Z</dc:date>
    </item>
  </channel>
</rss>

