<?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: AccessViolationException when using DockablePane when only groups are selected in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12871835#M4388</link>
    <description>&lt;P&gt;Currently, I have found such a solution:&lt;BR /&gt;- Subscribe to Panel.CollectionChanged at the "Modify" tab (to get: Autodesk.Windows.ComponentManager.Ribbon.FindTab("Modify"));&lt;BR /&gt;- During the event, I determine whether "Modify" tab contains panel with Id == "Dialog_ElementGroup_EditGroupNew";&lt;BR /&gt;- if true, then for MFCPaneControl of my DockablePane (to get: UIFramework.MainWindow.getMainWnd().FindPaneControl(paneGuid)) for property IsEnabled set value false, else set true.&lt;/P&gt;&lt;P&gt;Also, if it contains this panel, then I forbid to show my DockablePane.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;UPD: Even that doesn't solve the problem 100%( I somehow managed to get exception with this&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jul 2024 08:02:18 GMT</pubDate>
    <dc:creator>andhxl</dc:creator>
    <dc:date>2024-07-03T08:02:18Z</dc:date>
    <item>
      <title>AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12870082#M4387</link>
      <description>&lt;P&gt;If click anywhere inside&amp;nbsp;DockablePane when only groups are in the selection, and then switch to another view by clicking on tab, will Revit crash (shown in the video).&lt;/P&gt;&lt;P&gt;&lt;div class="lia-vid-container video-embed-center"&gt;&lt;div id="lia-vid-6356079951112w960h540r330" class="lia-video-brightcove-player-container"&gt;&lt;video-js data-video-id="6356079951112" data-account="6057940548001" data-player="default" data-embed="default" class="vjs-fluid" controls="" data-application-id="" style="width: 100%; height: 100%;"&gt;&lt;/video-js&gt;&lt;/div&gt;&lt;script src="https://players.brightcove.net/6057940548001/default_default/index.min.js"&gt;&lt;/script&gt;&lt;script&gt;(function() {  var wrapper = document.getElementById('lia-vid-6356079951112w960h540r330');  var videoEl = wrapper ? wrapper.querySelector('video-js') : null;  if (videoEl) {     if (window.videojs) {       window.videojs(videoEl).ready(function() {         this.on('loadedmetadata', function() {           this.el().querySelectorAll('.vjs-load-progress div[data-start]').forEach(function(bar) {             bar.setAttribute('role', 'presentation');             bar.setAttribute('aria-hidden', 'true');           });         });       });     }  }})();&lt;/script&gt;&lt;a class="video-embed-link" href="https://forums.autodesk.com/t5/video/gallerypage/video-id/6356079951112"&gt;(view in My Videos)&lt;/a&gt;&lt;/div&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This also happens if: only groups are selected -&amp;gt;&amp;nbsp;show&amp;nbsp;&lt;SPAN&gt;custom DockablePane&lt;/SPAN&gt;&amp;nbsp;-&amp;gt; switch to another view by clicking on tab.&lt;/P&gt;&lt;P&gt;It does not depend on the specific plugin. This happens with all custom DockablePane.&lt;/P&gt;&lt;P&gt;This bug is definitely in version 2020 and 2021.&lt;/P&gt;&lt;P&gt;I have not found this bug since version 2024.&lt;/P&gt;&lt;P&gt;Exception System.AccessViolationException in Revit.&lt;/P&gt;&lt;P&gt;How to fix it?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jul 2024 14:07:48 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12870082#M4387</guid>
      <dc:creator>andhxl</dc:creator>
      <dc:date>2024-07-08T14:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12871835#M4388</link>
      <description>&lt;P&gt;Currently, I have found such a solution:&lt;BR /&gt;- Subscribe to Panel.CollectionChanged at the "Modify" tab (to get: Autodesk.Windows.ComponentManager.Ribbon.FindTab("Modify"));&lt;BR /&gt;- During the event, I determine whether "Modify" tab contains panel with Id == "Dialog_ElementGroup_EditGroupNew";&lt;BR /&gt;- if true, then for MFCPaneControl of my DockablePane (to get: UIFramework.MainWindow.getMainWnd().FindPaneControl(paneGuid)) for property IsEnabled set value false, else set true.&lt;/P&gt;&lt;P&gt;Also, if it contains this panel, then I forbid to show my DockablePane.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;UPD: Even that doesn't solve the problem 100%( I somehow managed to get exception with this&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 08:02:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12871835#M4388</guid>
      <dc:creator>andhxl</dc:creator>
      <dc:date>2024-07-03T08:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12874418#M4389</link>
      <description>&lt;P&gt;That's a fun Exception, looks like was fixed in Revit 2023 (Revit 2022 still breaks).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tested with my plugin that has a DockablePanel and in there have a TextBox, if I select that and the selection have a group like in your video, Revit breaks. I don't know if only breaks in the TextBox or other visual inside the Dockable gonna break as well.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because that problem does not happen in the newer version of Revit, kinda was fixed already.&lt;BR /&gt;And the steps is so specific, need to select a group then select the dockable pane and change the view. I wonder what you are doing to trigger that.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;You kinda can use the &lt;A href="https://www.revitapidocs.com/2019/916ab1bb-45a0-8cbf-482f-5c652dc1b06d.htm" target="_blank" rel="noopener"&gt;EditorInteraction&lt;/A&gt; to unselect the elements when interaction with the dockable pane, could be a way to 'fix'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 18:22:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12874418#M4389</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-07-02T18:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12874700#M4390</link>
      <description>&lt;P&gt;EditorInteraction with EditorInteractionType.Dismiss is not working correctly. After switching views, the selection is no longer reset (shown in the video).&lt;/P&gt;&lt;P&gt;&lt;div class="lia-vid-container video-embed-center"&gt;&lt;div id="lia-vid-6356341424112w960h540r74" class="lia-video-brightcove-player-container"&gt;&lt;video-js data-video-id="6356341424112" data-account="6057940548001" data-player="default" data-embed="default" class="vjs-fluid" controls="" data-application-id="" style="width: 100%; height: 100%;"&gt;&lt;/video-js&gt;&lt;/div&gt;&lt;script src="https://players.brightcove.net/6057940548001/default_default/index.min.js"&gt;&lt;/script&gt;&lt;script&gt;(function() {  var wrapper = document.getElementById('lia-vid-6356341424112w960h540r74');  var videoEl = wrapper ? wrapper.querySelector('video-js') : null;  if (videoEl) {     if (window.videojs) {       window.videojs(videoEl).ready(function() {         this.on('loadedmetadata', function() {           this.el().querySelectorAll('.vjs-load-progress div[data-start]').forEach(function(bar) {             bar.setAttribute('role', 'presentation');             bar.setAttribute('aria-hidden', 'true');           });         });       });     }  }})();&lt;/script&gt;&lt;a class="video-embed-link" href="https://forums.autodesk.com/t5/video/gallerypage/video-id/6356341424112"&gt;(view in My Videos)&lt;/a&gt;&lt;/div&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This sequence of actions can actually happen quite often (unlike the second case, which I wrote about). DockablePane of my plugin is constantly displayed to users. The specifics of its purpose (as a project browser). In real work for the whole working day, such situations can occur quite often. Several users have sent feedback about the crash when using the plugin. I looked in the revit journal what they were doing before the crash and everyone select group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The solution I wrote above (with temporary disable MFCPaneControl and banning showing DockablePane) works at 95%. It suits me, but I still hope for a better solution&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 07:20:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12874700#M4390</guid>
      <dc:creator>andhxl</dc:creator>
      <dc:date>2024-07-03T07:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12874852#M4391</link>
      <description>&lt;P&gt;That's annoying that the &lt;EM&gt;EditorInteractionType.Dismiss&lt;/EM&gt; does not work...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I kinda found a workaround by deselecting all the elements in the&amp;nbsp;&lt;STRONG&gt;ViewActivating&lt;/STRONG&gt; and selecting after the view finish to change in the &lt;STRONG&gt;ViewActivated&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;At least Revit does not break anymore, but I don't know if that approach gonna have some issue when you have too many elements selected and change the view, probably would make more sense to only do that if selection have only groups.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is the basic code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;

namespace RevitAddin.Revit
{
    public class AppView : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication application)
        {
            application.ViewActivating += Application_ViewActivating;
            application.ViewActivated += Application_ViewActivated;
            return Result.Succeeded;
        }

        public Result OnShutdown(UIControlledApplication application)
        {
            application.ViewActivating -= Application_ViewActivating;
            application.ViewActivated -= Application_ViewActivated;
            return Result.Succeeded;
        }

        ICollection&amp;lt;ElementId&amp;gt; elementIds;
        private void Application_ViewActivating(object sender, Autodesk.Revit.UI.Events.ViewActivatingEventArgs e)
        {
            var uiapp = sender as UIApplication;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Selection selection = uidoc.Selection;
            elementIds = selection.GetElementIds();
            selection.SetElementIds(new ElementId[] { });
        }

        private void Application_ViewActivated(object sender, Autodesk.Revit.UI.Events.ViewActivatedEventArgs e)
        {
            var uiapp = sender as UIApplication;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Selection selection = uidoc.Selection;
            selection.SetElementIds(elementIds);
            elementIds = new ElementId[] { };
        }
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Maybe if you use the &lt;STRONG&gt;ViewActivating&lt;/STRONG&gt; event to force your &lt;STRONG&gt;DockablePane&lt;/STRONG&gt; to lose focus, maybe that do the trick.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 23:59:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12874852#M4391</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-07-02T23:59:38Z</dc:date>
    </item>
    <item>
      <title>Re: AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12875754#M4392</link>
      <description>&lt;P&gt;Thanks, the option with selection works. Lost focus didn't work for me.&lt;BR /&gt;As a result, it turned out something like this:&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using Autodesk.Windows;
using System.Diagnostics;

/// &amp;lt;summary&amp;gt;
/// Helper to work around a Revit bug (prior to Revit 2023) that can cause a crash (AccessViolationException) during certain
/// interactions involving a DockablePane and a selection containing only groups.
///
/// The crash can be reproduced in the following scenarios:
///
/// Scenario 1 (pane is visible):
/// 1) The current selection contains only groups;
/// 2) Click anywhere inside the DockablePane;
/// 3) Switch to another view via a view tab.
///
/// Scenario 2 (pane was hidden):
/// 1) The current selection contains only groups;
/// 2) Show the DockablePane;
/// 3) Switch to another view via a view tab.
///
/// After calling &amp;lt;see cref="Enable"/&amp;gt; (for Revit &amp;amp;lt; 2023), this helper temporarily clears the selection in &amp;lt;c&amp;gt;ViewActivating&amp;lt;/c&amp;gt;
/// and restores it in &amp;lt;c&amp;gt;ViewActivated&amp;lt;/c&amp;gt;, preventing the crash in scenario 1.
///
/// To prevent the crash in scenario 2, call &amp;lt;see cref="CheckCrashPossible(UIControlledApplication)"/&amp;gt; before showing the pane:
/// if the method returns &amp;lt;c&amp;gt;true&amp;lt;/c&amp;gt;, the pane must not be shown.
///
/// Semaphore is used to prevent multiple instances of this helper from being enabled simultaneously within the same Revit process.
/// &amp;lt;/summary&amp;gt;
public sealed class DockablePaneHelper(UIControlledApplication uiapp) : IDisposable
{
    private static readonly RibbonTab _modifyTab = ComponentManager.Ribbon.FindTab("Modify");
    private const string GroupPanelId = "Dialog_ElementGroup_EditGroupNew";

    private readonly UIControlledApplication _uiapp = uiapp ?? throw new ArgumentNullException(nameof(uiapp));
    private readonly bool _hasBug = CheckHasBug(uiapp);

    private ICollection&amp;lt;ElementId&amp;gt; _selectedIds;
    private bool _restoreSelection;

    private static readonly string _semaphoreName = $"{nameof(DockablePaneHelper)}.{GetCurrentProcessId()}";
    private Semaphore _semaphore;

    public bool IsEnabled { get; private set; }

    public static bool CheckCrashPossible(UIControlledApplication uiapp)
    {
        if (uiapp is null) throw new ArgumentNullException(nameof(uiapp));
        return CheckHasBug(uiapp) &amp;amp;&amp;amp; CheckCrashPossible();
    }

    private static bool CheckHasBug(UIControlledApplication uiapp)
    {
        // The bug was fixed in Revit 2023
        return int.Parse(uiapp.ControlledApplication.VersionNumber) &amp;lt; 2023;
    }

    private static bool CheckCrashPossible()
    {
        // Known crash cases exist when only groups are selected
        // Finding the panel that appears when only groups are selected is faster than searching for groups in the current selection
        return _modifyTab.FindPanel(GroupPanelId) is not null;
    }

    public void Enable()
    {
        if (IsEnabled || !_hasBug) return;
        if (!TryAcquireSemaphore()) return;

        _uiapp.ViewActivating += OnViewActivatingClearSelection;
        _uiapp.ViewActivated += OnViewActivatedRestoreSelection;

        IsEnabled = true;
    }

    public void Disable()
    {
        if (!IsEnabled) return;

        _uiapp.ViewActivating -= OnViewActivatingClearSelection;
        _uiapp.ViewActivated -= OnViewActivatedRestoreSelection;

        IsEnabled = false;

        ReleaseSemaphore();
    }

    public void Dispose() =&amp;gt; Disable();

    private void OnViewActivatingClearSelection(object sender, ViewActivatingEventArgs e)
    {
        if (!CheckCrashPossible()) return;

        var uiapp = (UIApplication)sender;
        var uidoc = uiapp.ActiveUIDocument;
        if (uidoc is null) return;

        var selection = uidoc.Selection;
        _selectedIds = selection.GetElementIds();
        selection.SetElementIds([]);

        _restoreSelection = true;
    }

    private void OnViewActivatedRestoreSelection(object sender, ViewActivatedEventArgs e)
    {
        if (!_restoreSelection) return;

        var uiapp = (UIApplication)sender;
        uiapp.ActiveUIDocument?.Selection.SetElementIds(_selectedIds);

        _selectedIds = null;
        _restoreSelection = false;
    }

    private bool TryAcquireSemaphore()
    {
        var semaphore = new Semaphore(1, 1, _semaphoreName);
        if (!semaphore.WaitOne(0))
        {
            semaphore.Dispose();
            return false;
        }
        _semaphore = semaphore;
        return true;
    }

    private void ReleaseSemaphore()
    {
        _semaphore.Release();
        _semaphore.Dispose();
        _semaphore = null;
    }

    private static int GetCurrentProcessId()
    {
        using var p = Process.GetCurrentProcess();
        return p.Id;
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&lt;A href="https://gist.github.com/andhxl/fcecddea2b64a845cf0702b3bf6faa18" target="_blank" rel="noopener"&gt;https://gist.github.com/andhxl/fcecddea2b64a845cf0702b3bf6faa18&lt;/A&gt;&lt;/P&gt;&lt;P&gt;UPD:&amp;nbsp;Added a version check, thanks&lt;/P&gt;</description>
      <pubDate>Tue, 10 Mar 2026 09:53:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12875754#M4392</guid>
      <dc:creator>andhxl</dc:creator>
      <dc:date>2026-03-10T09:53:46Z</dc:date>
    </item>
    <item>
      <title>Re: AccessViolationException when using DockablePane when only groups are selected</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12875869#M4393</link>
      <description>&lt;P&gt;Neat! I like your service implementation looks similar how I do it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One thing I would add is to only enable/start this service if version of Revit is 2022 or lower.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something like this inside the Start to not enable the service if the version is 2023 or greater.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;// The bug is fixed in Revit 2023
if (int.TryParse(_uiApp.ControlledApplication.VersionNumber, out int versionNumber) &amp;amp;&amp;amp; versionNumber &amp;gt;= 2023)
    return false;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Without that the &lt;STRONG&gt;SelectionChanged&lt;/STRONG&gt; event trigger again if you change the view, and &lt;STRONG&gt;SelectionChanged&lt;/STRONG&gt; was added in 2023 the same year this problem does not happen anymore.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 11:36:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/accessviolationexception-when-using-dockablepane-when-only/m-p/12875869#M4393</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2024-07-03T11:36:22Z</dc:date>
    </item>
  </channel>
</rss>

