<?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: Run multiple tasks async?? in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047875#M6449</link>
    <description>&lt;P data-unlink="true"&gt;I tried to run 2 exports using your above Background method and I got the GPU error in ArcGIS Pro.&amp;nbsp; I don't think it was a coincidence.&amp;nbsp; I tried this with the same layout to see if I could process multiple exports simultaniously.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;Task btt = BackgroundTaskTemp.Run(TaskPriority.normal, () =&amp;gt; testlayout.Export(PDF));&lt;BR /&gt;Task btt2 = BackgroundTaskTemp.Run(TaskPriority.normal, () =&amp;gt; testlayout.Export(PDF2));&lt;BR /&gt;await btt;&lt;BR /&gt;await btt2;&lt;/P&gt;&lt;P data-unlink="true"&gt;This above is essentially what I am trying to do.&amp;nbsp; A user selects multiple polygons, and I will export those as Maps to print.&amp;nbsp; They can select 1 to 10 maps.&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 15 Apr 2021 22:13:29 GMT</pubDate>
    <dc:creator>MKa</dc:creator>
    <dc:date>2021-04-15T22:13:29Z</dc:date>
    <item>
      <title>Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046009#M6428</link>
      <description>&lt;P&gt;I am attempting to update a layout template by iterating through the text elements and changing the values of the template to something about the users map/settings.&lt;/P&gt;&lt;P&gt;I am able to loop through and change all the text elements, but I want to do all of the text elements at the same time.&amp;nbsp; I have a list of all the elements and the changes I want to make to each of them, but they always seem to run syncronously instead of in parellel?&amp;nbsp; What am i missing here?&amp;nbsp; The SetTextProperties is slow so I want to do about al 50 updates in parellel?&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; //I have a list of object TextElementUpdate and updates to them
var tasks = new List&amp;lt;Task&amp;gt;();
                foreach (TextElementUpdate teu in textElementUpdates)
                {
//Shouldn't this line happen instantly and the whenall will wait?
//This line I want in parellel, so like 50 running at once, instead it is sync
                    tasks.Add(teu.UpdateTextElement());
                }

                //Wait for all the tasks to finish
                await Task.WhenAll(tasks);


//-------------------------------------------------------------------
class TextElementUpdate
        {
            public TextElementUpdate(TextElement textElement, TextProperties textProperties)
            {
                TextElementToUpdate = textElement;
                NewTextProperties = textProperties;
            }
            public TextElement TextElementToUpdate { get; set; }
            public TextProperties NewTextProperties { get; set; }

            public Task UpdateTextElement()
            {
                return QueuedTask.Run(() =&amp;gt;
                {
                    TextElementToUpdate.SetTextProperties(NewTextProperties);
                });
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Apr 2021 17:15:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046009#M6428</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-12T17:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046091#M6430</link>
      <description>&lt;P&gt;Also there could be a way around this if maybe I could update all the textelements for a layout with one call?&amp;nbsp; Instead of looping through them and changing them.&amp;nbsp; Can't seem to be able to do that in bulk.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Apr 2021 20:34:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046091#M6430</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-12T20:34:21Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046346#M6432</link>
      <description>&lt;P&gt;I cant get this to work async?&amp;nbsp; I have a list of text elements from a layout (my print template), then loop through all of those textElements, get the name of the element, and change the text for that element to something from the users selected polygon attributes.&lt;/P&gt;&lt;P&gt;Is there a way to mass or bulk update the text elements?&amp;nbsp; i have to loop through and update about 50 text items for a layout template .pagx file.&amp;nbsp; I have tried updating the text properties and just using the CIMTextGraphic, and both seem very slow (around 40 seconds to do them all in total.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;List&amp;lt;TextElementUpdate&amp;gt; textElementUpdates = new List&amp;lt;TextElementUpdate&amp;gt;();
foreach (Element ele in inLayoutElements)
{
	if (ele is TextElement)
	{
		TextElement textElement = ele as TextElement;
		string printTextValue = "";
		if (string.Equals(textElement.Name, "MyFieldName", StringComparison.OrdinalIgnoreCase))
		{
			printTextValue = "PolygonAttribute";
		}
		else if (string.Equals(textElement.Name, "MyFieldName2", StringComparison.OrdinalIgnoreCase))
		{
			printTextValue = "PolygonAttribute2";
		}
		//Repeat for all 50 fields or so
		//...
		//...

//After each element is found add the change to the object to process all at once after the loop
		CIMTextGraphic CIMTextGraphic = textElement.Graphic as CIMTextGraphic;
		CIMTextGraphic.Text = printTextValue;
		TextElementUpdate teu = new TextElementUpdate(textElement, CIMTextGraphic);
		textElementUpdates.Add(teu);
	}
}

//Update All the TextElements, this is slow to process about 50 text elements 
foreach (TextElementUpdate teu in textElementUpdates)
{
	teu.TextElementToUpdate.SetGraphic(teu.NewTextGraphic);
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 13 Apr 2021 14:17:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046346#M6432</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-13T14:17:53Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046466#M6434</link>
      <description>&lt;P&gt;you might be better off going against the layout CIM definition. See the code snippet below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;  var layout = LayoutView.Active?.Layout;
  if (layout == null)
    return;
  await QueuedTask.Run(() =&amp;gt;
  {
    var def = layout.GetDefinition() as CIMLayout;
    var graphic_elements = def.Elements.OfType&amp;lt;CIMGraphicElement&amp;gt;().ToList() 
          ??  new List&amp;lt;CIMGraphicElement&amp;gt;();
    bool changed = false;
    var when = DateTime.Now.ToString("G");
    int c = 0;
    foreach (var ge in graphic_elements)
    {
      if (ge.Graphic is CIMTextGraphic textGraphic) 
      {
           textGraphic.Text = $"Changed {c++}, {when}";
           changed = true;
      }
     }
     if (changed)
       //commit the changes
       layout.SetDefinition(def);
   });&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If u want to find out more about the CIM take a look at this video:&lt;/P&gt;&lt;P&gt;&lt;A title="ArcGIS Pro SDK for .NET: Understanding the CIM, a Guide for Developers" href="https://www.youtube.com/watch?v=MP90p-jkA5Q&amp;amp;list=PLN5okgNxFYJo_gcO1snNlEFMHDwxdlwFU&amp;amp;index=8" target="_self"&gt;ArcGIS Pro SDK for .NET: Understanding the CIM, a Guide for Developers&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 17:22:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046466#M6434</guid>
      <dc:creator>CharlesMacleod</dc:creator>
      <dc:date>2021-04-13T17:22:08Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046598#M6436</link>
      <description>&lt;P&gt;That worked brilliantly.&amp;nbsp; Exactly what I needed.&amp;nbsp; I couldn't figure out how to update the elements and set them all at once using set definition.&amp;nbsp; Now i need to find a way to export 10 layouts quicker.&amp;nbsp; I am looping through say 10 polygons, adding the feature attributes to my layout template (using the great code above) and exporting them.&amp;nbsp;&amp;nbsp;This below export function will be my hang up now, as each call to export takes say (20-30 secs)&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;private static async Task ExportLayoutToFile(LayoutProjectItem inLayoutItem, string inExportFileName)
        {
            try
            {
                await QueuedTask.Run(() =&amp;gt;
                {
                    Layout testlayout = inLayoutItem.GetLayout();
                    if (testlayout == null)
                    {
                        return;
                    }

                    // Create PDF format with appropriate settings
                    PDFFormat PDF = new PDFFormat()
                    {
                        Resolution = 300,
                        OutputFileName = inExportFileName
                    };

                    if (PDF.ValidateOutputFilePath())
                    {
//THIS LINE IS MY HANG UP as I can't export multipls files at once?
                        testlayout.Export(PDF);
                    }
                });
            }
            catch (Exception e)
            {
                LogError("PrintForm - ExportLayoutToFile - ", e);
                return;
            }
        }&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 13 Apr 2021 20:07:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046598#M6436</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-13T20:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046637#M6437</link>
      <description>&lt;P&gt;export looks like a good candidate for the BackgroundTask so u might want to consider using _that_ rather than the QueuedTask. Obviously switching threads doesnt make something _quicker_ but it will free up the QueuedTask to allow u to get on with your other business.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;  var layout = LayoutView.Active?.Layout;
  if (layout == null)
    return;
  await QueuedTask.Run(() =&amp;gt;
  {
    var def = layout.GetDefinition() as CIMLayout;
    //Make changes...
    //Commit changes
    layout.SetDefinition(def);

    //Tee-up export - add-in is responsible for checking
    //validity of OutputFileName
    var pdf = new PDFFormat()
    {
      Resolution = 300,
      OutputFileName = $"C:\\temp\\{layout.Name}.pdf"
    };
    //Export on a background thread
    ArcGIS.Core.Threading.Tasks.BackgroundTask.Run(
       ArcGIS.Core.Threading.Tasks.TaskPriority.normal,
       () =&amp;gt; layout.Export(pdf),
       ArcGIS.Core.Threading.Tasks.BackgroundProgressor.None);
   //We are still on the QueuedTask here...
   //continue doing work but be mindful that your layout may
   //still be exporting
   ...
  });


    

&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;U can read more about it here:&amp;nbsp;&lt;A href="https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Asynchronous-Programming-in-ArcGIS-Pro#using-backgroundtask" target="_self"&gt;https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Asynchronous-Programming-in-ArcGIS-Pro#using-backgroundtask&lt;/A&gt;&amp;nbsp;. The most important thing to remember when using BackgroundTask is to never change application state_. That is what the CIM thread, or MCT as we call it is for, and that is accessed via QueuedTask.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Apr 2021 20:53:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046637#M6437</guid>
      <dc:creator>CharlesMacleod</dc:creator>
      <dc:date>2021-04-13T20:53:40Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046896#M6443</link>
      <description>&lt;P&gt;This will be great to run in the background.&amp;nbsp; However i cannot update past 2.5 yet.&amp;nbsp; We plan on doing that real soon, but that is not an option right now.&amp;nbsp; I might have to do a work around until we upgrade to 2.7 here in the next couple months.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 13:54:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1046896#M6443</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-14T13:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047113#M6444</link>
      <description>&lt;P&gt;BackgroundTask is 2.6 fyi.&lt;/P&gt;&lt;P&gt;In the meanwhile u can try something like this - note: u cant use a Task directly because Tasks are MTA. Anyway, make sure u read all the fine print. Dont run anything off the MCT that alters the application.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;//http://stackoverflow.com/questions/16720496/set-apartmentstate-on-a-task
	internal class BackgroundTaskTemp
	{

		public static Task Run(TaskPriority priority, Action action)
		{
			var tcs = new TaskCompletionSource&amp;lt;bool&amp;gt;();
			Thread thread = new Thread(() =&amp;gt;
			{
				try
				{
					action();
					tcs.SetResult(true);
				}
				catch (Exception e)
				{
					tcs.SetException(e);
				}
			});

			thread.Priority = FromTaskPriority(priority);
			thread.SetApartmentState(ApartmentState.STA);
			thread.Start();
			return tcs.Task;

		}

		public static Task&amp;lt;T&amp;gt; Run&amp;lt;T&amp;gt;(TaskPriority priority, Func&amp;lt;T&amp;gt; func)
		{
			var tcs = new TaskCompletionSource&amp;lt;T&amp;gt;();
			Thread thread = new Thread(() =&amp;gt;
			{
				try
				{
					tcs.SetResult(func());
				}
				catch (Exception e)
				{
					tcs.SetException(e);
				}
			});

			thread.Priority = FromTaskPriority(priority);
			thread.SetApartmentState(ApartmentState.STA);
			thread.Start();
			return tcs.Task;
		}

		private static ThreadPriority FromTaskPriority(TaskPriority priority)
		{
			if (priority == TaskPriority.high)
				return ThreadPriority.Highest;
			if (priority == TaskPriority.single)
				throw new InvalidOperationException("Sorry, no can do - wait for 2.6 and the proper BackgroundTask");
			else return ThreadPriority.Normal;
		}
	}
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;usage:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; var layout = LayoutView.Active?.Layout;
 if (layout == null)
   return;
 await QueuedTask.Run(() =&amp;gt;
 {
   ...
   //export on the background thread				 
   //ArcGIS.Core.Threading.Tasks.BackgroundTask.Run(
   //ArcGIS.Core.Threading.Tasks.TaskPriority.normal,
   //	() =&amp;gt; layout.Export(pdf),
   //ArcGIS.Core.Threading.Tasks.BackgroundProgressor.None);

   BackgroundTaskTemp.Run(				 
     ArcGIS.Core.Threading.Tasks.TaskPriority.normal,
    () =&amp;gt; layout.Export(pdf));
    ...
    ...
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 16:58:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047113#M6444</guid>
      <dc:creator>CharlesMacleod</dc:creator>
      <dc:date>2021-04-14T16:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047638#M6447</link>
      <description>&lt;P&gt;This looks very promising to what i want to do.&amp;nbsp; The thing is, is that I might be updating say 1-10 layouts at a time, so what I want to do is export them all at once on their own background thread, then wait for them all to finish.&amp;nbsp; So i think the only way to do that would be to save my "Updated" layout as a clone or something.&amp;nbsp; Otherwise I will just be printing the last layout 10 times?&amp;nbsp;&lt;/P&gt;&lt;P&gt;So right now, in a loop i update the layout using polygon attributes, then export it, then move to the next.&amp;nbsp; The export being the choke point.&amp;nbsp; So for 10 exports it's 10x time.&lt;/P&gt;&lt;P&gt;What i think your above process will let me do, is build up 10 layout clones in that loop, then export them all at once, drastically reducing the export time to 1x or 2x time.&amp;nbsp; But I think I need to clone the layouts that I update the elements on, then export them, then dispose them.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 16:04:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047638#M6447</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-15T16:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: Run multiple tasks async??</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047875#M6449</link>
      <description>&lt;P data-unlink="true"&gt;I tried to run 2 exports using your above Background method and I got the GPU error in ArcGIS Pro.&amp;nbsp; I don't think it was a coincidence.&amp;nbsp; I tried this with the same layout to see if I could process multiple exports simultaniously.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;Task btt = BackgroundTaskTemp.Run(TaskPriority.normal, () =&amp;gt; testlayout.Export(PDF));&lt;BR /&gt;Task btt2 = BackgroundTaskTemp.Run(TaskPriority.normal, () =&amp;gt; testlayout.Export(PDF2));&lt;BR /&gt;await btt;&lt;BR /&gt;await btt2;&lt;/P&gt;&lt;P data-unlink="true"&gt;This above is essentially what I am trying to do.&amp;nbsp; A user selects multiple polygons, and I will export those as Maps to print.&amp;nbsp; They can select 1 to 10 maps.&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;P data-unlink="true"&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 22:13:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/run-multiple-tasks-async/m-p/1047875#M6449</guid>
      <dc:creator>MKa</dc:creator>
      <dc:date>2021-04-15T22:13:29Z</dc:date>
    </item>
  </channel>
</rss>

