<?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: How to know if Revit API is in Context. in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12575426#M7009</link>
    <description>&lt;P&gt;Nice work&lt;/P&gt;</description>
    <pubDate>Thu, 22 Feb 2024 07:33:00 GMT</pubDate>
    <dc:creator>moturi.magati.george</dc:creator>
    <dc:date>2024-02-22T07:33:00Z</dc:date>
    <item>
      <title>How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12574320#M7008</link>
      <description>&lt;P&gt;&lt;SPAN&gt;After messing with Revit API for so long I figured out a reliable way to know if your code is in Revit API Context or not.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;By default, Revit API throws exceptions if your code is trying to execute Revit API methods in a WPF modeless view for example, that's the reason you need to use &lt;STRONG&gt;ExternalEvent&lt;/STRONG&gt; to execute Revit API code in context.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Sometimes it is needed to know if the code is running in context or if not, to just execute the Revit API code right away or send it to &lt;STRONG&gt;ExternalEvent&lt;/STRONG&gt; to be executed.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If you have access to &lt;STRONG&gt;UIApplication&lt;/STRONG&gt; or &lt;STRONG&gt;UIControllerApplication&lt;/STRONG&gt;, and if you try to subscribe to an event outside Revit API context you are gonna have this exception: Invalid call to Revit API! Revit is currently not within an API context.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Meaning you could use that to know if your code is in context or not.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ricaun_2-1708538347775.png" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1328835i763393F58D58D100/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ricaun_2-1708538347775.png" alt="ricaun_2-1708538347775.png" /&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here is a code sample and video: &lt;/SPAN&gt;&lt;A href="https://ricaun.com/revit-api-context/" target="_blank"&gt;&lt;SPAN&gt;https://ricaun.com/revit-api-context/&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ricaun_0-1708539573548.gif" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1328842iBE72ABC2F7DD12F8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ricaun_0-1708539573548.gif" alt="ricaun_0-1708539573548.gif" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;div class="video-embed-center video-embed"&gt;&lt;iframe class="embedly-embed" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F4Pr2uJILaso%3Ffeature%3Doembed&amp;amp;display_name=YouTube&amp;amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D4Pr2uJILaso&amp;amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F4Pr2uJILaso%2Fhqdefault.jpg&amp;amp;type=text%2Fhtml&amp;amp;schema=youtube" width="200" height="112" scrolling="no" title="What is Revit API Context?" frameborder="0" allow="autoplay; fullscreen; encrypted-media; picture-in-picture" allowfullscreen="true"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'm this technique using my open-source library to manage the creation of ExternalEvent if it is not in context and enable it to run Revit API asynchronously. &lt;/SPAN&gt;&lt;A href="https://github.com/ricaun-io/ricaun.Revit.UI.Tasks" target="_blank"&gt;&lt;SPAN&gt;https://github.com/ricaun-io/ricaun.Revit.UI.Tasks&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ricaun_1-1708539573548.gif" style="width: 600px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1328843i67FF6E08894E44F0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ricaun_1-1708539573548.gif" alt="ricaun_1-1708539573548.gif" /&gt;&lt;/span&gt;&lt;div class="video-embed-center video-embed"&gt;&lt;iframe class="embedly-embed" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fgyo6xGN5DDU%3Ffeature%3Doembed&amp;amp;display_name=YouTube&amp;amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dgyo6xGN5DDU&amp;amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fgyo6xGN5DDU%2Fhqdefault.jpg&amp;amp;type=text%2Fhtml&amp;amp;schema=youtube" width="200" height="112" scrolling="no" title="Tasks &amp;amp; InContext - Revit API" frameborder="0" allow="autoplay; fullscreen; encrypted-media; picture-in-picture" allowfullscreen="true"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I'm planning to create a full tutorial in the future about this library on my &lt;A href="http://ricaun.com/youtube" target="_blank" rel="noopener"&gt;YouTube&lt;/A&gt; channel.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Feb 2024 18:22:23 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12574320#M7008</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-02-21T18:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12575426#M7009</link>
      <description>&lt;P&gt;Nice work&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2024 07:33:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12575426#M7009</guid>
      <dc:creator>moturi.magati.george</dc:creator>
      <dc:date>2024-02-22T07:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12631804#M7010</link>
      <description>&lt;P&gt;Shared here on the blog:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/2024/03/api-context-aps-toolkit-and-da4r-debugging.html#2" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/2024/03/api-context-aps-toolkit-and-da4r-debugging.html#2&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Mar 2024 10:15:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12631804#M7010</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2024-03-11T10:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12964070#M7011</link>
      <description>&lt;P&gt;The Preview version of &lt;STRONG&gt;RevitToolkit&lt;/STRONG&gt; provides a way to do this in a more efficient way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Throwing an exception takes quite a bit of time, and creating additional empty &lt;STRONG&gt;EventHandler&amp;lt;IdlingEventArgs&amp;gt;&lt;/STRONG&gt; increases memory allocation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Toolkit now has &lt;STRONG&gt;Context.IsRevitInApiMode&lt;/STRONG&gt; property, that determines whether Revit is in API mode or not&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Benchamarks:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;IsRevitInApiMode&lt;/EM&gt;&lt;BR /&gt;Exception median: 24122.4 ns&lt;BR /&gt;Native median: 413.1 ns&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;EM&gt;IsRevitOtsideApiMode&lt;/EM&gt;&lt;BR /&gt;Exception median: 9522530.7 ns&lt;BR /&gt;Native median: 550 ns&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The difference in speed is 58 times in context, and 17313 outside (1000 iterations)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2025.0.2-preview.1.0 is available for testing&lt;/P&gt;&lt;P&gt;&lt;A href="https://github.com/Nice3point/RevitToolkit#context" target="_blank" rel="noopener"&gt;https://github.com/Nice3point/RevitToolkit#context&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 00:48:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12964070#M7011</guid>
      <dc:creator>nice3point</dc:creator>
      <dc:date>2024-08-17T00:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12964099#M7012</link>
      <description>&lt;P&gt;Neat!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A method inside the &lt;STRONG&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;APIUIAPI&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/STRONG&gt; that does the context check. I wonder what others useful stuff is in that assembly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for sharing! &lt;span class="lia-unicode-emoji" title=":clapping_hands:"&gt;👏&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 01:26:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12964099#M7012</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-08-17T01:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12983555#M7013</link>
      <description>&lt;P&gt;Thank you for discovering and sharing this. Those are impressive results indeed. Shared on the blog as well:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://thebuildingcoder.typepad.com/blog/2024/08/api-context-background-process-postcommand.html#4" target="_blank"&gt;https://thebuildingcoder.typepad.com/blog/2024/08/api-context-background-process-postcommand.html#4&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Aug 2024 11:22:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/12983555#M7013</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2024-08-27T11:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012476#M7014</link>
      <description>&lt;P&gt;Looks like the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is linked with the &lt;STRONG&gt;Revit API Context&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I never realized it, but the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is always null when outside the &lt;STRONG&gt;Revit API Context&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;bool addInContext = uiapp.ActiveAddInId is not null;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It makes sense that your code would always need an &lt;STRONG&gt;AddIn Context&lt;/STRONG&gt; to execute Revit code. Some Revit API methods only allows a specific &lt;STRONG&gt;AddIn&lt;/STRONG&gt; to interact, like in the &lt;STRONG&gt;IUpdater&lt;/STRONG&gt; and &lt;STRONG&gt;ExtensibleStorage&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did some benchmark and looks similar like the &lt;STRONG&gt;IsRevitInApiMode &lt;/STRONG&gt;implementation. &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4158637"&gt;@nice3point&lt;/a&gt; how did you create your benchamark, do you have some sample? I did a simple one using &lt;STRONG&gt;Stopwatch.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2024 20:23:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012476#M7014</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-09-10T20:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012504#M7015</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4176855"&gt;@ricaun&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Nice variant, however is it possible to guarantee that this property is 100% context related ? And additionally it requires a reference to the&lt;STRONG&gt; UiAppication&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;I'm unlikely to find a benchmark, but yes, I've used &lt;STRONG&gt;StopWatch&lt;/STRONG&gt; that way, and additionally 100 iterations on ColdStart ( it doesn't include iteration time in the result), because of the need for Jit compilation, which affects the first startup.&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2024 20:38:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012504#M7015</guid>
      <dc:creator>nice3point</dc:creator>
      <dc:date>2024-09-10T20:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012683#M7016</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4158637"&gt;@nice3point&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Nice variant, however is it possible to guarantee that this property is 100% context related ?&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/824630"&gt;@jeremy_tammik&lt;/a&gt;could you confirm with the dev team that the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is always available when Revit is in context.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't think in a example in Revit that is not true.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Only in DA4R that &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is null, but is a bug found recently: &lt;A href="https://forums.autodesk.com/t5/revit-api-forum/revit-design-automation-extensible-storage-quot-writing-of/td-p/12833018" target="_blank" rel="noopener"&gt;https://forums.autodesk.com/t5/revit-api-forum/revit-design-automation-extensible-storage-quot-writing-of/td-p/12833018&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4158637"&gt;@nice3point&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;And additionally it requires a reference to the&lt;STRONG&gt; UiAppication&lt;/STRONG&gt;.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Is kinda easy to get the &lt;STRONG&gt;UIApplication&lt;/STRONG&gt; especially if you want to run a Revit code, and &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is available in the &lt;STRONG&gt;UIControlledApplication&lt;/STRONG&gt;, &lt;STRONG&gt;ControlledApplication&lt;/STRONG&gt; and &lt;STRONG&gt;Application&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4158637"&gt;@nice3point&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;I'm unlikely to find a benchmark, but yes, I've used &lt;STRONG&gt;StopWatch&lt;/STRONG&gt; that way, and additionally 100 iterations on ColdStart ( it doesn't include iteration time in the result), because of the need for Jit compilation, which affects the first startup.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Nice, I created this test project with all 3 implementations:&amp;nbsp;&lt;A href="https://github.com/ricaun-io/RevitTest.InContext.Tests" target="_blank" rel="noopener"&gt;https://github.com/ricaun-io/RevitTest.InContext.Tests&lt;/A&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;InAddInContext&lt;/STRONG&gt;: Using &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;InApiMode&lt;/STRONG&gt;: Using &lt;STRONG&gt;APIUIAPI&lt;/STRONG&gt;&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;InContext&lt;/STRONG&gt;:&amp;nbsp; Using &lt;STRONG&gt;Idling&lt;/STRONG&gt; exception&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;The &lt;STRONG&gt;InAddInContext&lt;/STRONG&gt; and &lt;STRONG&gt;InApiMode&lt;/STRONG&gt; have a similar time, &lt;STRONG&gt;&lt;EM&gt;0.001 ms&lt;/EM&gt;&lt;/STRONG&gt; to &lt;EM&gt;&lt;STRONG&gt;0.002 ms&lt;/STRONG&gt;&lt;/EM&gt;, and &lt;STRONG&gt;InContext&lt;/STRONG&gt; is around 25 to 40 slower.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I gonna try to find some cases to check if &lt;STRONG&gt;ActiveAddInId &lt;/STRONG&gt;is available inside Revit events or something, but I'm pretty sure if &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; could be use to check the Revit API Context.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2024 22:44:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012683#M7016</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-09-10T22:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012715#M7017</link>
      <description>&lt;P&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4176855"&gt;@ricaun&lt;/a&gt;&amp;nbsp;&lt;A href="https://github.com/ricaun-io/RevitTest.InContext.Tests" target="_blank" rel="noopener nofollow noreferrer"&gt;&lt;STRONG&gt;RevitTest.InContext.Tests&lt;/STRONG&gt;&lt;/A&gt; is a private repository for now, could you open it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Benchmarks, remember to use Release mode:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public class StartupCommand : ExternalCommand
{
    private const int ColdStartIterations = 200;
    private const int TargetIterations = 3000;
    private readonly Stopwatch _monitor = new();

    public override void Execute()
    {
        List&amp;lt;Func&amp;lt;bool&amp;gt;&amp;gt; targets =
        [
            IsRevitInApiMode,
            IsAddinActive,
            IsIdling
        ];

        Console.WriteLine();
        Console.WriteLine("Inside context");

        ExecuteTargets(targets);
        
        Console.WriteLine();
        Console.WriteLine("Outside context");
        
        Task.Run(() =&amp;gt; ExecuteTargets(targets));
    }

    private void ExecuteTargets(List&amp;lt;Func&amp;lt;bool&amp;gt;&amp;gt; targets)
    {
        foreach (var target in targets)
        {
            RunBenchmark(ColdStartIterations, target);

            _monitor.Start();
            RunBenchmark(TargetIterations, target);
            _monitor.Stop();

            Console.WriteLine($"{target.Method.Name}: {_monitor.Elapsed.TotalNanoseconds / TargetIterations} ns.");
            _monitor.Reset();
        }
    }

    private static void RunBenchmark(int iterations, Func&amp;lt;bool&amp;gt; target)
    {
        for (var i = 0; i &amp;lt; iterations; i++)
        {
            try
            {
                target.Invoke();
            }
            catch
            {
                //ignored
            }
        }
    }

    private static bool IsRevitInApiMode()
    {
        return Context.IsRevitInApiMode;
    }

    private static bool IsAddinActive()
    {
        return Context.UiApplication.ActiveAddInId is not null;
    }

    private static bool IsIdling()
    {
        try
        {
            Context.UiApplication.Idling += OnApplicationIdling;
            Context.UiApplication.Idling -= OnApplicationIdling;
            return true;

            void OnApplicationIdling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
            {
            }
        }
        catch
        {
            return false;
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Inside context&lt;BR /&gt;IsRevitInApiMode: 277 ns.&lt;BR /&gt;IsAddinActive: 465 ns.&lt;BR /&gt;IsIdling: 15910 ns.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Outside context&lt;BR /&gt;IsRevitInApiMode: 303 ns.&lt;BR /&gt;IsAddinActive: 536 ns.&lt;BR /&gt;IsIdling: 20945 ns.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 00:23:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13012715#M7017</guid>
      <dc:creator>nice3point</dc:creator>
      <dc:date>2024-09-11T00:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013282#M7018</link>
      <description>&lt;P&gt;Sure; I &lt;A href="https://autodesk.slack.com/archives/C0SR6NAP8/p1726037584408059" target="_blank"&gt;asked&lt;/A&gt; the development team.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 06:55:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013282#M7018</guid>
      <dc:creator>jeremy_tammik</dc:creator>
      <dc:date>2024-09-11T06:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013753#M7019</link>
      <description>&lt;P&gt;My bad, I change to public. &lt;A href="https://github.com/ricaun-io/RevitTest.InContext.Tests" target="_blank" rel="noopener nofollow noreferrer"&gt;&lt;STRONG&gt;RevitTest.InContext.Tests&lt;/STRONG&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 11:12:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013753#M7019</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-09-11T11:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013834#M7020</link>
      <description>&lt;P&gt;I add a test&amp;nbsp;&lt;A href="https://github.com/ricaun-io/RevitTest.InContext.Tests/blob/develop/RevitTest.InContext.Tests/ToolkitTests.cs" target="_blank" rel="noopener"&gt;&lt;STRONG&gt;ToolkitTests&lt;/STRONG&gt; &lt;/A&gt;similar like your &lt;STRONG&gt;StartupCommand &lt;/STRONG&gt;but the results looks a little strange. &lt;STRONG&gt;IsRevitInApiMode &lt;/STRONG&gt;looks slow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Inside context&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsRevitInApiMode: 55981.6 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsAddinActive: 858.6 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsIdling: 27201.3 ns.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Outside context&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsRevitInApiMode: 52370.4 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsAddinActive: 756.3 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsIdling: 33753.9 ns.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Edited: Add link to the &lt;A href="https://github.com/ricaun-io/RevitTest.InContext.Tests/blob/develop/RevitTest.InContext.Tests/ToolkitTests.cs" target="_blank" rel="noopener"&gt;ToolkitTests.cs&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 11:57:10 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013834#M7020</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-09-11T11:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013897#M7021</link>
      <description>&lt;P&gt;RevitLookUp uses a Toolkit version without &lt;STRONG&gt;IsRevitInApiMode&lt;/STRONG&gt;, an exception was happening.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;System.MissingMethodException: Method not found: 'Boolean Nice3point.Revit.Toolkit.Context.get_IsRevitInApiMode()'.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now with the correct version looks better.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Inside context&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsRevitInApiMode: 394.2 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsAddinActive: 744.7 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsIdling: 21207.7 ns.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Outside context&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsRevitInApiMode: 379.7 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsAddinActive: 744.0 ns.&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;IsIdling: 36853.7 ns.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 12:25:45 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013897#M7021</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-09-11T12:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013938#M7022</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/4176855"&gt;@ricaun&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;RevitLookUp uses a Toolkit version without &lt;STRONG&gt;IsRevitInApiMode&lt;/STRONG&gt;, an exception was happening.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Yes, that's the first thing I thought, Lookup is now using a previous version of Toolkit, you can run the dependency update tool &lt;A href="https://github.com/jeremytammik/RevitLookup/issues/269" target="_blank"&gt;https://github.com/jeremytammik/RevitLookup/issues/269&lt;/A&gt; but I see everything is already in order.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;IsRevitInApiMode looks a bit faster because it doesn't contain additional calls to auxiliary functions like ActiveAddin. And if the development team confirms your theory, ActiveAddin may well be used for developers who don't use Toolkit&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 12:38:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13013938#M7022</guid>
      <dc:creator>nice3point</dc:creator>
      <dc:date>2024-09-11T12:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13014039#M7023</link>
      <description>&lt;P&gt;Yes, I just updated manually &lt;STRONG&gt;Nice3point.Revit.Toolkit.dll&lt;/STRONG&gt; version in the &lt;STRONG&gt;RevitLookup &lt;/STRONG&gt;folder.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only think I don't like about the &lt;STRONG&gt;IsRevitInApiMode&lt;/STRONG&gt; is that uses the &lt;STRONG&gt;APIUIAPI.dll&lt;/STRONG&gt;, if was &lt;STRONG&gt;RevitAPI.dll&lt;/STRONG&gt; or &lt;STRONG&gt;RevitAPIUI.dll&lt;/STRONG&gt; would be perfect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope I'm correct, and is kinda simpler to explain that you need an AddIn Context to run a Revit code, with &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; null is kinda obvious that Revit could reject the code without valid AddIn Context.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 13:25:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13014039#M7023</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-09-11T13:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13014154#M7024</link>
      <description>&lt;P&gt;The same thing could be called in RevitAPIUI, but will require more coding, APIUIAPI calls the method directly, with less latency, so this option is more performant and easier to understand for code maintenance&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 13:59:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13014154#M7024</guid>
      <dc:creator>nice3point</dc:creator>
      <dc:date>2024-09-11T13:59:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to know if Revit API is in Context.</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13276039#M7025</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Revit AddIn Context&lt;/STRONG&gt; is what I'm calling the Revit API Context now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested in a lot of places inside the Revit API and the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is always valid because Revit tracks what &lt;STRONG&gt;AddInId&lt;/STRONG&gt; is executing the code.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Some methods requires to have an &lt;STRONG&gt;AddInId&lt;/STRONG&gt; like the Extensible Storage, and when registering a &lt;STRONG&gt;IExternalCommand&lt;/STRONG&gt; in a panel the command always generate the same &lt;STRONG&gt;AddInId&lt;/STRONG&gt; that was used to register the command.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the basic code to check if Revit is in the AddIn Context: &lt;A href="https://ricaun.com/revit-addin-context/" target="_blank"&gt;https://ricaun.com/revit-addin-context/&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;bool InAddInContext(UIApplication application)
{
    // ActiveAddInId is null when invoke outside Revit Api context.
    return application.ActiveAddInId is not null;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;div class="video-embed-center video-embed"&gt;&lt;iframe class="embedly-embed" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbQUFxfSMI6E%3Ffeature%3Doembed&amp;amp;display_name=YouTube&amp;amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbQUFxfSMI6E&amp;amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbQUFxfSMI6E%2Fhqdefault.jpg&amp;amp;type=text%2Fhtml&amp;amp;schema=youtube" width="200" height="112" scrolling="no" title="ActiveAddInId == AddIn Context - Revit API" frameborder="0" allow="autoplay; fullscreen; encrypted-media; picture-in-picture" allowfullscreen="true"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is only one place that the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; is null and Revit API in in Context, inside the Design Automation for Revit event. That's is a bug/limitation and what made me looking in to the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; propriety closer.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://forums.autodesk.com/t5/revit-api-forum/revit-design-automation-extensible-storage-quot-writing-of/td-p/12833018" target="_blank"&gt;https://forums.autodesk.com/t5/revit-api-forum/revit-design-automation-extensible-storage-quot-writing-of/td-p/12833018&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;And because now I know how the &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt; works, I can use &lt;STRONG&gt;ExternalService&lt;/STRONG&gt; inside Design Automation for Revit to make the event to run in a valid &lt;STRONG&gt;ActiveAddInId&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I created a library for Design Automation for Revit to fix that and another issue I'm having inside DA4R.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://github.com/ricaun-io/ricaun.Revit.DA" target="_blank"&gt;https://github.com/ricaun-io/ricaun.Revit.DA&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just need to create a video on&amp;nbsp;&lt;SPAN&gt;&lt;A href="http://ricaun.com/youtube" target="_blank" rel="nofollow noopener noreferrer"&gt;YouTube&lt;/A&gt;&lt;/SPAN&gt; about the package.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2025 14:29:30 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/how-to-know-if-revit-api-is-in-context/m-p/13276039#M7025</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2025-01-22T14:29:30Z</dc:date>
    </item>
  </channel>
</rss>

