Select to view content in your preferred language

How to avoid crash when add-in has a visible DockPane and cannot initialize?

359
8
a month ago
mahj
by
Occasional Contributor

If an ArcGIS Pro add-in has a DockPane that is currently visible and the add-in's overridden Module.Initialize() method returns false because it for some reason cannot initialize properly, ArcGIS Pro crashes.

How can that be avoided? I have tried to hide the DockPane in either Module.Initialize() or Module.Uninitialize(), but nothing seems to help.

I'm using ArcGIS Pro 3.5.3.

Steps to reproduce:

1. Start Visual Studio 2022 (17.14.18 in my case)
2. Create new project of type ArcGIS Pro Module Add-in
3. Right-click project in Solution Explorer, select Add->New Item...
4. In the Add New Item dialog select 'ArcGIS Pro Dockpane' and click Add
5. Press Ctrl+F5 to build and run the project
6. When ArcGIS Pro starts create a new Map project
7. Click on the Add-In tab and click the 'Show Dockpane 1' button in 'Group 1'
8. Press Ctrl+S to save the project and close ArcGIS Pro
9. Open Module1.cs in Solution Explorer, override the Initialize() method, and make it return false.
10. Press Ctrl+F5 to build and run the project
11. When ArcGIS Pro starts, open the saved project
12. ArcGIS Pro crashes and displays the "ArcGIS Application has stopped working" dialog

Thanks for any help!

0 Kudos
8 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

I would recommend to close dockpane before closing ArcGIS Pro. Start listen  ProjectClosingEvent in your dockpane:

        /// <summary>
        /// Override to implement custom initialization code for this dockpane
        /// </summary>
        /// <returns></returns>
        protected override async Task InitializeAsync()
        {
                await base.InitializeAsync();

                ProjectClosingEvent.Subscribe(OnProjectClosing);
        }

And on project closing close your dockpane:

        private Task OnProjectClosing(ProjectClosingEventArgs args)
        {
            // if already cancelled, ignore
            if (args.Cancel)
                return Task.CompletedTask;

            var vm = FrameworkApplication.DockPaneManager.Find(_dockPaneID);
            if (vm != null) vm.Hide();

            return Task.CompletedTask;
        }
0 Kudos
mahj
by
Occasional Contributor

Yes, thanks, that's one possibility.

But it's a bit inconvenient for the users, its like closing the Catalog pane for them whenever they close ArcGIS Pro. I would rather just close it if the add-in cannot initialize properly, and keep it open for them across sessions otherwise.

0 Kudos
GKmieliauskas
Esri Regular Contributor

Why do you need to return false from Module.Initialize()? There are many ways to disable functionality without returning false: conditions and etc.

0 Kudos
mahj
by
Occasional Contributor

For a few reasons.

Reading the docs it looked like it is the recommended way to handle the case when the pre-requisites for an add-in to load isn't fulfilled. In this case there are several add-ins which should not be loaded if a certain configuration or other add-ins they are dependent on aren't loaded.

It would also be the easiest way to know whether a dependency is not available since FrameworkApplication.FindModule() returns null if the add-in's module returns false from Initialize(). Otherwise another mechanism to detect when an add-in couldn't be loaded must be implemented and spread throughout the development team.

0 Kudos
GKmieliauskas
Esri Regular Contributor

You can manage add-ins dependencies on each other inside daml. ArcGIS Pro will manage order of loading your add-ins on information stored in daml.

0 Kudos
mahj
by
Occasional Contributor

Yes, thanks, I know. But it's not the loading order that is the problem, it's avoiding that ArcGIS Pro crashes when some of them cannot load successfully.

0 Kudos
UmaHarano
Esri Regular Contributor

Hi @mahj 

One solution can be to:

  • Set the Autoload attribute of the addin to be true. You set this is in your addins's DAML. This allows the Module to initialize when Pro starts up.

  • In your Module Initialization callback, before you return false, get the Dockpane and set its IsVisible property to false.

 protected override bool Initialize()
 {
   //TODO: Be sure to add your business logic conditions for returning false, etc.
   DockPane pane = FrameworkApplication.DockPaneManager.Find("ProAppModule3_Dockpane1");
   if (pane != null)
     pane.IsVisible = false; 
   return false;
 }​

 

mahj
by
Occasional Contributor

Thanks, that worked!

But since the functionality in the add-ins are not always used, it would be good to lazy-load them to keep startup time shorter.

Any plans to make this work without setting autoLoad to true?

0 Kudos