<?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: Recursion and QueuedTask.Run - help!! in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231174#M9065</link>
    <description>&lt;P&gt;You're right it does work. I have simplified what I have done - so I guess I'll take it from there and build it up in complexity and see what is causing the problem.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Nov 2022 10:54:57 GMT</pubDate>
    <dc:creator>Vidar</dc:creator>
    <dc:date>2022-11-14T10:54:57Z</dc:date>
    <item>
      <title>Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231082#M9061</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm trying to go through each layer in the TOC and do get the alias name of the layer then do something that name. In order to this - I have a recursive method that loops - however you need to&lt;STRONG&gt; await QueuedTask.Run()&lt;/STRONG&gt; the call to get information about an alias in layer - and in a recursive function something weird is happening - as this only gets called once and never breaks into that Queued Task code block again.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something is happening here thats a bit beyond me when it gets to asynchronous programming - can anyone help me find a way to get this recursive function to work properly?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;//calling code:
var map = MapView.Active.Map;
foreach (var item in map.Layers)
{
    await RecurseTocLayers(item);
}



private async Task RecurseTocLayers(Layer layer)
        {
            if (layer is BasicFeatureLayer)
            {
                FeatureLayer featureLayer = layer as FeatureLayer;

                //THIS ONLY GETS CALLED ONCE - THEN NEVER AGAIN!
                await QueuedTask.Run(() =&amp;gt;
                {
                    var table = featureLayer.GetTable();
                    TableDefinition tableDefinition = table.GetDefinition();
                    string alias = tableDefinition.GetAliasName();
                    //do something here with alias name.....e.g.
                    System.Diagnostics.Debug.WriteLine($"The alias name is: {alias}");
                });
            }


            if (layer is GroupLayer)
            {
                //do something here with GroupLayer, then....

                GroupLayer groupLayer = (GroupLayer)layer;
                foreach (var child in groupLayer.Layers)
                {
                    await RecurseTocLayers(child);
                }
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ASDFDSAF&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 09:35:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231082#M9061</guid>
      <dc:creator>Vidar</dc:creator>
      <dc:date>2022-11-14T09:35:29Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231138#M9062</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Code you pasted has a different count of opening and closing brackets, but it works fine. Could you please share code you read layers list from TOC?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 06:24:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231138#M9062</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2022-11-14T06:24:46Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231163#M9063</link>
      <description>&lt;P&gt;Updated - not really anything special to call code.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 09:36:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231163#M9063</guid>
      <dc:creator>Vidar</dc:creator>
      <dc:date>2022-11-14T09:36:03Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231164#M9064</link>
      <description>&lt;P&gt;Code below works:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;    internal class Button1 : Button
    {
        protected async override void OnClick()
        {
            var layers = MapView.Active.Map.GetLayersAsFlattenedList();

            foreach(var layer in layers) {
                await RecurseTocLayers(layer);
            }
        }

        private async Task RecurseTocLayers(Layer layer)
        {
            if (layer is BasicFeatureLayer)
            {
                FeatureLayer featureLayer = layer as FeatureLayer;

                //THIS ONLY GETS CALLED ONCE - THEN NEVER AGAIN!
                await QueuedTask.Run(() =&amp;gt;
                {
                    var table = featureLayer.GetTable();
                    TableDefinition tableDefinition = table.GetDefinition();
                    string alias = tableDefinition.GetAliasName();
                    //do something here with alias name.....e.g.
                    System.Diagnostics.Debug.WriteLine($"The alias name is: {alias}");
                });
            }

	        if (layer is GroupLayer)
	        {
		        //do something here with GroupLayer, then....
		
		        GroupLayer groupLayer = (GroupLayer)layer;
		        foreach (var child in groupLayer.Layers)
		        {
			        await RecurseTocLayers(child);
                }
            }
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 09:47:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231164#M9064</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2022-11-14T09:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231174#M9065</link>
      <description>&lt;P&gt;You're right it does work. I have simplified what I have done - so I guess I'll take it from there and build it up in complexity and see what is causing the problem.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 10:54:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231174#M9065</guid>
      <dc:creator>Vidar</dc:creator>
      <dc:date>2022-11-14T10:54:57Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231180#M9066</link>
      <description>&lt;P&gt;Does my code work on your environment?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 10:53:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231180#M9066</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2022-11-14T10:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231188#M9067</link>
      <description>&lt;P&gt;It does actually. However the code I left out was a line that created a row in a database (a method that is not async) based on findings earlier on i.e. the alias name. I think this may be the bit of code that is causing the code to not run properly.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Nov 2022 11:18:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1231188#M9067</guid>
      <dc:creator>Vidar</dc:creator>
      <dc:date>2022-11-14T11:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Recursion and QueuedTask.Run - help!!</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1232043#M9078</link>
      <description>&lt;P&gt;You should surround your code with try {} catch {} to see what errors you get.&amp;nbsp; &amp;nbsp;I can see at least one bug in this snippet:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;if (layer is BasicFeatureLayer)
            {
                FeatureLayer featureLayer = layer as FeatureLayer;

                //THIS ONLY GETS CALLED ONCE - THEN NEVER AGAIN!
                await QueuedTask.Run(() =&amp;gt;
                {
                    var table = featureLayer.GetTable();
                    TableDefinition tableDefinition = table.GetDefinition();
                    string alias = tableDefinition.GetAliasName();
                    //do something here with alias name.....e.g.
                    System.Diagnostics.Debug.WriteLine($"The alias name is: {alias}");
                });
            }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that AnnotationLayer,&amp;nbsp;DimensionLayer and&amp;nbsp;FeatureLayer all derive from&amp;nbsp;BasicFeatureLayer.&amp;nbsp; So in your code you check if layer is of the type 'BasicFeatureLayer' and if true you assign the casted layer to the featureLayer variable:&lt;/P&gt;&lt;P&gt;var&amp;nbsp;featureLayer = layer as FeatureLayer;&lt;/P&gt;&lt;P&gt;Needless to say, your featureLayer variable will be null for Dimension or Annotation layers.&amp;nbsp; Consequently, your code is using featureLayer without checking for null which will throw an exception.&lt;/P&gt;&lt;P&gt;The code snippet will work for maps with no Dimension or Annotation layers.&lt;/P&gt;</description>
      <pubDate>Wed, 16 Nov 2022 00:08:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/recursion-and-queuedtask-run-help/m-p/1232043#M9078</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2022-11-16T00:08:23Z</dc:date>
    </item>
  </channel>
</rss>

