<?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: Can someone please explain this ConstructedOnWrongThreadException? in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581314#M12570</link>
    <description>&lt;P&gt;Thank you, &lt;SPAN&gt;GKmieliauskas&lt;/SPAN&gt;.&amp;nbsp; But as usual, I wrote too much and obfuscated my own question.&amp;nbsp; My brief "NB FYI" comment indicates that we already plan to refactor our GetExtent method exactly as your second example shows.&amp;nbsp; I'll restate my question here . . .&lt;BR /&gt;&lt;BR /&gt;Why does the GeometryEngine class's Expand(...) method need to be called in QueuedTask.Run?&amp;nbsp; Neither its &lt;A href="https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic8225.html" target="_self"&gt;doc&lt;/A&gt;&amp;nbsp;nor the SDK via VisualStudio's intellisense says anything about that need.&amp;nbsp; And why don't I get a &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Called&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;OnWrongThreadException for that?&amp;nbsp; I get a&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;&lt;U&gt;Constructed&lt;/U&gt;&lt;/EM&gt;&lt;/STRONG&gt;OnWrongThreadException (we've caused &amp;amp; addressed the former exception many times, but this is my first time seeing the latter) despite my code not constructing anything.&lt;/P&gt;</description>
    <pubDate>Mon, 03 Feb 2025 15:13:13 GMT</pubDate>
    <dc:creator>DanNarsavage_IDWR</dc:creator>
    <dc:date>2025-02-03T15:13:13Z</dc:date>
    <item>
      <title>Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1580842#M12567</link>
      <description>&lt;P&gt;I had this method (simplified but I think it provides all the necessary context) . . .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public override Envelope GetExtent()
{
    var geometry = GetGeometry();
    return GeometryEngine.Instance.Expand(geometry.Extent, 1.5, 1.5, true);
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I used that inside Pro, I was treated to an exception with this stack trace . . .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;ArcGIS.Core.ConstructedOnWrongThreadException: This object must be created within the lambda passed to QueuedTask.Run, or on a compatible STA thread.
   at ArcGIS.Core.CoreObjectsBase.ValidateThread()
   at ArcGIS.Core.Internal.Geometry.EnvelopeBuilder..ctor(Envelope envelope, Boolean getHandle)
   at ArcGIS.Core.Internal.Geometry.EnvelopeBuilder..ctor(Envelope envelope)
   at ArcGIS.Core.Geometry.GeometryEngine.Expand(Envelope envelope, Double dx, Double dy, Boolean asRatio)
   at My.Namespace.MyClass.GetExtent() in D:\Whatever\MyClass.cs:line 106
   at My.Namespace.MyOtherClass.AnotherMethod() in D:\Whatever\MyOtherClass.cs:line 167&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found that this avoids that exception . . .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public override Envelope GetExtent()
{
    var geometry = GetGeometry();
    var extent = geometry.Extent;
    var geometryEngine = GeometryEngine.Instance;
    var expandTask = QueuedTask.Run(() =&amp;gt; geometryEngine.Expand(extent, 1.5, 1.5, true);)
    return expandTask.Result;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I'm not constructing anything within that QueuedTask.&amp;nbsp; I think this is just a leaky abstraction in the API, but I'm still somewhat new to async and fear I'm missing something, so I'd appreciate anyone pointing out what I'm constructing or why I've gotta run that method (whose &lt;A title="Doc for Expand method" href="https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic8225.html" target="_blank" rel="noopener"&gt;doc&lt;/A&gt; doesn't call out the need to call it on the MCT) in a QueuedTask.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 22:09:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1580842#M12567</guid>
      <dc:creator>DanNarsavage_IDWR</dc:creator>
      <dc:date>2025-01-31T22:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1580844#M12568</link>
      <description>&lt;P&gt;NB FYI we will eventually modify that method to `public override async Task&amp;lt;Envelope&amp;gt; GetExtent()`, but we need other parts of our solution to stabilize first--this is a migration from ArcObjects.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 15:05:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1580844#M12568</guid>
      <dc:creator>DanNarsavage_IDWR</dc:creator>
      <dc:date>2025-01-31T15:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581137#M12569</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Take a look at these:&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Asynchronous-Programming-in-ArcGIS-Pro" target="_self"&gt;https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Asynchronous-Programming-in-ArcGIS-Pro&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.youtube.com/watch?v=oUh509rbUew" target="_blank"&gt;ArcGIS Pro SDK for .NET: Synchronous and Asynchronous Custom Method Design - YouTube&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Shortly. If you want something return from asynchronous method you need to use Task&amp;lt;return_data_type&amp;gt;.&lt;/P&gt;&lt;P&gt;That method could contain QueuedTask.Run or orther .NET methos which returns Task.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can make synchronous method and call QueuedTask.Run outside method. Below two samples with your mehod implemetation:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public Envelope GetExtent()
{
    var geometry = GetGeometry();
    var extent = geometry.Extent;
    var geometryEngine = GeometryEngine.Instance;
    return geometryEngine.Expand(extent, 1.5, 1.5, true);
}

var newExtent = await QueuedTask.Run(() =&amp;gt;
{
   return GetExtent();
});&lt;/LI-CODE&gt;&lt;P&gt;or&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public Task&amp;lt;Envelope&amp;gt; GetExtentAsync()
{
    var geometry = GetGeometry();
    var extent = geometry.Extent;
    var geometryEngine = GeometryEngine.Instance;
    return QueuedTask.Run(() =&amp;gt;
    {
       return geometryEngine.Expand(extent, 1.5, 1.5, true);
    });
}

var newExtent = await GetExtentAsync();&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 02 Feb 2025 14:21:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581137#M12569</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2025-02-02T14:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581314#M12570</link>
      <description>&lt;P&gt;Thank you, &lt;SPAN&gt;GKmieliauskas&lt;/SPAN&gt;.&amp;nbsp; But as usual, I wrote too much and obfuscated my own question.&amp;nbsp; My brief "NB FYI" comment indicates that we already plan to refactor our GetExtent method exactly as your second example shows.&amp;nbsp; I'll restate my question here . . .&lt;BR /&gt;&lt;BR /&gt;Why does the GeometryEngine class's Expand(...) method need to be called in QueuedTask.Run?&amp;nbsp; Neither its &lt;A href="https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic8225.html" target="_self"&gt;doc&lt;/A&gt;&amp;nbsp;nor the SDK via VisualStudio's intellisense says anything about that need.&amp;nbsp; And why don't I get a &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Called&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;OnWrongThreadException for that?&amp;nbsp; I get a&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;&lt;U&gt;Constructed&lt;/U&gt;&lt;/EM&gt;&lt;/STRONG&gt;OnWrongThreadException (we've caused &amp;amp; addressed the former exception many times, but this is my first time seeing the latter) despite my code not constructing anything.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 15:13:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581314#M12570</guid>
      <dc:creator>DanNarsavage_IDWR</dc:creator>
      <dc:date>2025-02-03T15:13:13Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581393#M12571</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/462024"&gt;@DanNarsavage_IDWR&lt;/a&gt;&amp;nbsp;Yes, you are right. Expand methos doesn't need to be run within QueuedTask.Run.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think issue is with your returned geometry (from GetGeometry method) extent.&amp;nbsp; Try to check extent for null and for IsEmpty.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 17:26:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581393#M12571</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2025-02-03T17:26:24Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581450#M12573</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/42133"&gt;@GKmieliauskas&lt;/a&gt;&amp;nbsp;In all my experience, GeometryEngine.Expand(...) &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;does&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&amp;nbsp;need to be called from a QueuedTask, unless the goal is to receive a ConstructedOnWrongThreadException. To demonstrate this more clearly, I've modified my method again, this time using modified portions of the &lt;A href="https://github.com/Esri/arcgis-pro-sdk/blob/master/Examples/Geometry/ProSnippets.cs#L1865" target="_self"&gt;"ConstructEnvelope()" ProSnippets method&lt;/A&gt;&amp;nbsp;to make an Envelope from scratch without hiding anything up my sleeve.&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;public override Envelope GetExtent()
{
    var sr = SpatialReferenceBuilder.CreateSpatialReference(8826);
    MapPoint minPt = MapPointBuilderEx.CreateMapPoint(2456480, 1307526, sr);
    MapPoint maxPt = MapPointBuilderEx.CreateMapPoint(2456895, 1307790, sr);
    EnvelopeBuilderEx builderEx = new EnvelopeBuilderEx(minPt, maxPt, sr);
    Envelope envelope = builderEx.ToGeometry();
    var geometryEngine = GeometryEngine.Instance;
    var expandedExtent = geometryEngine.Expand(envelope, 1.5, 1.5, true);
    return expandedExtent;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Running that still results in a ConstructedOnWrongThreadException...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;ArcGIS.Core.ConstructedOnWrongThreadException: This object must be created within the lambda passed to QueuedTask.Run, or on a compatible STA thread.
   at ArcGIS.Core.CoreObjectsBase.ValidateThread()
   at ArcGIS.Core.Internal.Geometry.EnvelopeBuilder..ctor(Envelope envelope, Boolean getHandle)
   at ArcGIS.Core.Internal.Geometry.EnvelopeBuilder..ctor(Envelope envelope)
   at ArcGIS.Core.Geometry.GeometryEngine.Expand(Envelope envelope, Double dx, Double dy, Boolean asRatio)
   at MyProject.MyClass.GetExtent() in C:\Path\MyClass.cs:line 112
   at MyProject.ViewModels.MyViewModel.RunProcessUpdate(Boolean changeExtent) in C:\OtherPath\MyViewModel.cs:line 167&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Putting the Expand method in a QueuedTask--and making no other changes--produces the desired results.&amp;nbsp;&amp;nbsp;&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;public override Envelope GetExtent()
{
    var sr = SpatialReferenceBuilder.CreateSpatialReference(8826);
    MapPoint minPt = MapPointBuilderEx.CreateMapPoint(2456480, 1307526, sr);
    MapPoint maxPt = MapPointBuilderEx.CreateMapPoint(2456895, 1307790, sr);
    EnvelopeBuilderEx builderEx = new EnvelopeBuilderEx(minPt, maxPt, sr);
    Envelope envelope = builderEx.ToGeometry();
    var geometryEngine = GeometryEngine.Instance;
    var expandedExtent = QueuedTask.Run(() =&amp;gt; geometryEngine.Expand(envelope, 1.5, 1.5, true)).Result;
    return expandedExtent;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;No exceptions are thrown from that version of my GetExtent() method. So I'll drop the deference I've shown to the Pro SDK so far in this thread and say that I think one of these two things is true:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;The API for GeometryEngine.Expand(...) contains a bug. Something under the hood of that method should be doing its work on the MCT, but it is not.&lt;/LI&gt;&lt;LI&gt;The API doc for same is wrong. The doc should call out the fact that this method should be called within a QueuedTask.&lt;UL&gt;&lt;LI&gt;Of course, in this case the API&amp;nbsp;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;still&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt; contains a bug: it should be throwing a&amp;nbsp;&lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Called&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;OnWrongThreadException rather than a &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;Constructed&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;OnWrongThreadException.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Mon, 03 Feb 2025 21:04:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581450#M12573</guid>
      <dc:creator>DanNarsavage_IDWR</dc:creator>
      <dc:date>2025-02-03T21:04:00Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581691#M12574</link>
      <description>&lt;P&gt;We have proverb in my native language: "Butter buttered". So, the same thing is with Envelope and Expand method. Envelope has own Expand method:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;            var sr = SpatialReferenceBuilder.CreateSpatialReference(8826);
            MapPoint minPt = MapPointBuilderEx.CreateMapPoint(2456480, 1307526, sr);
            MapPoint maxPt = MapPointBuilderEx.CreateMapPoint(2456895, 1307790, sr);
            EnvelopeBuilderEx builderEx = new EnvelopeBuilderEx(minPt, maxPt, sr);
            Envelope envelope = builderEx.ToGeometry();
            var expandedExtent = envelope.Expand(1.5, 1.5, true);&lt;/LI-CODE&gt;&lt;P&gt;It works as expected without QueuedTask.Run. It could be a bug in GeometryEngine method.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2025 10:06:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581691#M12574</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2025-02-04T10:06:38Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581779#M12575</link>
      <description>&lt;P&gt;Thank you, that did the trick.&amp;nbsp; Not sure how I didn't notice that method in the first place.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2025 15:40:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1581779#M12575</guid>
      <dc:creator>DanNarsavage_IDWR</dc:creator>
      <dc:date>2025-02-04T15:40:45Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1585405#M12640</link>
      <description>&lt;P&gt;Hallo,&lt;/P&gt;&lt;P&gt;There seem to be similar problems in other contexts now (ArcGIS Pro 3.4.2). For example "OnSketchCompleteAsync", all passed sketch geometries contain "ConstructedOnWrongThreadExceptions" .&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="DirkTillmanns_0-1739527156640.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/125362i935336ED2C62BDF9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="DirkTillmanns_0-1739527156640.png" alt="DirkTillmanns_0-1739527156640.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Feb 2025 10:05:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1585405#M12640</guid>
      <dc:creator>DirkTillmanns</dc:creator>
      <dc:date>2025-02-14T10:05:17Z</dc:date>
    </item>
    <item>
      <title>Re: Can someone please explain this ConstructedOnWrongThreadException?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1592051#M12704</link>
      <description>&lt;P&gt;It is a bug in GeometryEngine, and the fix will be in the next release. Fortunately, you have the workaround in the meantime. Thank you for reporting this issue.&lt;/P&gt;&lt;P&gt;Annette&lt;/P&gt;</description>
      <pubDate>Tue, 04 Mar 2025 22:29:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/can-someone-please-explain-this/m-p/1592051#M12704</guid>
      <dc:creator>AnnetteLocke</dc:creator>
      <dc:date>2025-03-04T22:29:44Z</dc:date>
    </item>
  </channel>
</rss>

