<?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: Multithreading issue with QueuedTask.Run? in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590289#M12691</link>
    <description>&lt;P&gt;I do not have this option, whereby one can chain the calls to method1 and method2.&lt;BR /&gt;They are independent of each other and are called by different sections of the app independently.&lt;BR /&gt;Just that sometimes they may get called simultaneously within a multithreaded env. And that's when the conflict/issue seems to occur. Currently I am using slimsemaphore to lock across both methods so 2 QueuedTask bodies are not called simultaneously. But just thought this should be automatic. I am covering for these 2 methods... but there are other methods too. Do I need to handle locking across all methods..?&lt;BR /&gt;&lt;BR /&gt;Is there something I am not doing right in calling QueuedTask that would guarantee they run independent of each other... or is this conflict due to something else..? Why would&amp;nbsp;QueuedTask exit event in method1 cause any issues with&amp;nbsp;QueuedTask body execution in method2 if they are being executed simultaneously? Is there a parameter in&amp;nbsp;QueuedTask call to ensure independence without using custom semaphore/locking implementation?&lt;/P&gt;</description>
    <pubDate>Thu, 27 Feb 2025 19:40:16 GMT</pubDate>
    <dc:creator>Gurunara</dc:creator>
    <dc:date>2025-02-27T19:40:16Z</dc:date>
    <item>
      <title>Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1589400#M12681</link>
      <description>&lt;P&gt;Is QueuedTask.Run thread safe? Is it not intended for multi-threaded environment and we have to implement our own locking mechanism?&lt;BR /&gt;&lt;BR /&gt;Was getting weird error where:&lt;BR /&gt;&lt;BR /&gt;Have method1, and method2.&lt;BR /&gt;Each are async methods and within the body of the method, they have QueuedTask.Run(() =&amp;gt; { });&lt;/P&gt;&lt;P&gt;The thing is they may both run in a multithreaded context because they may get called&amp;nbsp;concurrently.&lt;BR /&gt;&lt;BR /&gt;When that happens, sequence of call may be:&lt;BR /&gt;&lt;BR /&gt;Method1 gets called, and while method1 is executing within the QueuedTask.Run body, method2 gets called and executes its&amp;nbsp;QueuedTask.Run body.&lt;BR /&gt;&lt;BR /&gt;Method2 exits the&amp;nbsp;QueuedTask.Run body first while method1 is still inside&amp;nbsp;QueuedTask.Run body.&lt;BR /&gt;&lt;BR /&gt;But it seems the exit of&amp;nbsp;QueuedTask.Run body by method2 is causing method1 to exit out of its&amp;nbsp;QueuedTask.Run body (or something)... method1 tries to execute one of the methods within its&amp;nbsp;QueuedTask.Run body, one of the built-in ArcPro SDK functions such as (LayerFactory.Instance.CreateGroupLayer(MapView.Active.Map, 0, containerName)) and it just hangs...&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;In debug mode, after stepping over the statement it just seem to exit out of the&amp;nbsp;QueuedTask.Run body or hang or something. It's stuck and have to stop the debug mode execution in VS. When run in non-debug mode, the ArcPro app just gets stuck and have to use task manager to stop the executable.&lt;BR /&gt;&lt;BR /&gt;Current workaround is to use custom&amp;nbsp;SemaphoreSlim implementation and acquire lock before the&amp;nbsp;QueuedTask.Run body is run on both methods, so that those 2 methods never run simultaneously...&lt;BR /&gt;&lt;BR /&gt;But there appears to be multiple method signatures for&amp;nbsp;QueuedTask.Run. Is there perhaps a different call signature for it that would avoid this issue or somthing..?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2025 12:20:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1589400#M12681</guid>
      <dc:creator>Gurunara</dc:creator>
      <dc:date>2025-02-26T12:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1589493#M12682</link>
      <description>&lt;P&gt;Have you looked into the&amp;nbsp;&lt;A href="https://github.com/Esri/arcgis-pro-sdk/wiki/ProConcepts-Asynchronous-Programming-in-ArcGIS-Pro#overview" target="_self"&gt;Asynchronous Programming in ArcGIS Pro&lt;/A&gt;?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2025 15:36:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1589493#M12682</guid>
      <dc:creator>Aashis</dc:creator>
      <dc:date>2025-02-26T15:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1589618#M12683</link>
      <description>&lt;P&gt;The documentation says that &lt;SPAN&gt;Queued Tasks run first in, first run methodology. However, that does not guarantee that they will complete in that order. To address this you can add code similar to that shown below to ensure your tasks are complete prior to moving on to your next step.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Task t1 = QueuedTask.Run(() =&amp;gt; MapView.Active.Map.SetSpatialReference(srWANad83Ft));&lt;BR /&gt;Task t2 = QueuedTask.Run(() =&amp;gt; MapView.Active.Redraw(false));&lt;BR /&gt;Task.WaitAll(t1, t2);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;or&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Task t1 = QueuedTask.Run(() =&amp;gt; MapView.Active.Map.SetSpatialReference(srWANad83Ft));&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;t1.Wait();&lt;BR /&gt;Task t2 = QueuedTask.Run(() =&amp;gt; MapView.Active.Redraw(false));&lt;BR /&gt;t2.Wait();&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;some would say you could just await the&amp;nbsp;&lt;SPAN&gt;QueuedTask; however, be aware that await is 'fire and forget' and does not actually cause a Wait to happen.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2025 18:34:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1589618#M12683</guid>
      <dc:creator>RichardDaniels</dc:creator>
      <dc:date>2025-02-26T18:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590289#M12691</link>
      <description>&lt;P&gt;I do not have this option, whereby one can chain the calls to method1 and method2.&lt;BR /&gt;They are independent of each other and are called by different sections of the app independently.&lt;BR /&gt;Just that sometimes they may get called simultaneously within a multithreaded env. And that's when the conflict/issue seems to occur. Currently I am using slimsemaphore to lock across both methods so 2 QueuedTask bodies are not called simultaneously. But just thought this should be automatic. I am covering for these 2 methods... but there are other methods too. Do I need to handle locking across all methods..?&lt;BR /&gt;&lt;BR /&gt;Is there something I am not doing right in calling QueuedTask that would guarantee they run independent of each other... or is this conflict due to something else..? Why would&amp;nbsp;QueuedTask exit event in method1 cause any issues with&amp;nbsp;QueuedTask body execution in method2 if they are being executed simultaneously? Is there a parameter in&amp;nbsp;QueuedTask call to ensure independence without using custom semaphore/locking implementation?&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 19:40:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590289#M12691</guid>
      <dc:creator>Gurunara</dc:creator>
      <dc:date>2025-02-27T19:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590311#M12692</link>
      <description>&lt;P&gt;&lt;SPAN&gt;QueuedTask is used to schedule tasks to run on the MCT (thread). Based on my reading, there is only &lt;STRONG&gt;One&lt;/STRONG&gt; MCT thread (please correct me if I'm wrong). They start in the order they are queued BUT we do not know when they will be done. As you say, you start method1, method2 but in some cases method2 is Completed before method1. That is why one can use Task.WaitAll() or Task.Wait() to ensure a result is available for the next Step in our code.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;One thing I did Today that appears to be working was refactor my code to reduce the number of nested&amp;nbsp;QueuedTasks and then used Return to explicitly return a result to avoid NullObjectExceptions.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;MapPoint mapPoint;&lt;BR /&gt;Task&amp;lt;MapPoint&amp;gt; t2a = QueuedTask.Run(() =&amp;gt;&lt;BR /&gt;{&lt;BR /&gt;MapPoint mapPoint0;&lt;BR /&gt;mapPoint0 = MapView.Active.ClientToMap(e.ClientPoint);&lt;BR /&gt;//add graphic to the map&lt;BR /&gt;AddPointGraphicAsync(mapPoint0);&lt;BR /&gt;return mapPoint0;&lt;BR /&gt;});&lt;BR /&gt;t2a.Wait();&lt;BR /&gt;mapPoint = (MapPoint) t2a.Result;&lt;BR /&gt;Task t2b = QueuedTask.Run(() =&amp;gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;//do more work and use the mapPoint for SelectingRowsFromATable, this requires a&amp;nbsp;QueuedTask&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;});&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2025 20:12:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590311#M12692</guid>
      <dc:creator>RichardDaniels</dc:creator>
      <dc:date>2025-02-27T20:12:55Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590629#M12698</link>
      <description>&lt;P&gt;found the real reason for the issue... this doesn't seem to be due to multiple queuedtask's being run:&lt;BR /&gt;&lt;BR /&gt;it was the following series of calls:&lt;BR /&gt;&lt;BR /&gt;if (workflowViewContainer is GroupLayer workflowViewGroupLayer)&lt;BR /&gt;MapView.Active.Map.MoveLayer(workflowViewGroupLayer, 0);&lt;BR /&gt;if (workflowContainer is GroupLayer workflowGroupLayer)&lt;BR /&gt;MapView.Active.Map.MoveLayer(workflowGroupLayer, 0);&lt;BR /&gt;if (coreAssetFSContainer is GroupLayer coreAssetFSGroupLayer)&lt;BR /&gt;MapView.Active.Map.MoveLayer(coreAssetFSGroupLayer, 0);&lt;BR /&gt;if (workflowLocContainer is GroupLayer workflowLocGroupLayer)&lt;BR /&gt;MapView.Active.Map.MoveLayer(workflowLocGroupLayer, 0);&lt;BR /&gt;&lt;BR /&gt;inside of queuedtask. it always hangs on one of these MoveLayer calls... i removed them and it starts working... i removed them and set the order initially in&amp;nbsp;LayerFactory.Instance.CreateGroupLayer instead.&lt;BR /&gt;&lt;BR /&gt;there seems to be something wrong (not quite right) with&amp;nbsp;MapView.Active.Map.MoveLayer...&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2025 13:19:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590629#M12698</guid>
      <dc:creator>Gurunara</dc:creator>
      <dc:date>2025-02-28T13:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590816#M12699</link>
      <description>&lt;P&gt;does&amp;nbsp;&lt;SPAN&gt;MapView.Active.Map.MoveLayer(workflowLocGroupLayer, 0); cause a redraw action on the screen? If so there is a possibility that first MoveLayer is not completed at the time your next call is made.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Feb 2025 17:33:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1590816#M12699</guid>
      <dc:creator>RichardDaniels</dc:creator>
      <dc:date>2025-02-28T17:33:14Z</dc:date>
    </item>
    <item>
      <title>Re: Multithreading issue with QueuedTask.Run?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1591053#M12700</link>
      <description>&lt;P&gt;Sometimes the first MoveLayer fails.&lt;BR /&gt;&lt;BR /&gt;However, the following seems to work:&lt;BR /&gt;&lt;BR /&gt;QueuedTask.Run(async () =&amp;gt;&lt;BR /&gt;{&lt;BR /&gt;...&lt;BR /&gt;await Task.Run(async () =&amp;gt; await QueuedTask.Run(() =&amp;gt; MapView.Active.Map.MoveLayer(workflowLocGroupLayer, 0)));&lt;BR /&gt;&lt;BR /&gt;});&lt;BR /&gt;&lt;BR /&gt;Wrapping in Task.Run seems to work. But since MoveLayer has to be run inside QueuedTask.Run, put in another QueuedTask inside.&lt;BR /&gt;&lt;BR /&gt;PS:&amp;nbsp;MapView.Active.Map.MoveStandaloneTable also seems to have this issue...&lt;/P&gt;</description>
      <pubDate>Sun, 02 Mar 2025 09:01:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/multithreading-issue-with-queuedtask-run/m-p/1591053#M12700</guid>
      <dc:creator>Gurunara</dc:creator>
      <dc:date>2025-03-02T09:01:45Z</dc:date>
    </item>
  </channel>
</rss>

