ArcGIS Pro 3.0 API Reference Guide
ArcGIS.Desktop.Framework.Contracts Namespace / Module Class / CanUnload Method
Example

In This Topic
    CanUnload Method
    In This Topic
    When overridden in a derived class, a Module can return that it can't be unloaded.
    Syntax
    protected internal virtual bool CanUnload()
    Protected Friend Overridable Function CanUnload() As Boolean
    Remarks
    If the Framework makes the determination that a Module should be unloaded, it will first call CanUnload to verify with the Module that it is okay to do so.
    Example
    Prevent ArcGIS Pro from Closing
    // There are two ways to prevent ArcGIS Pro from closing
    // 1. Override the CanUnload method on your add-in's module and return false.
    // 2. Subscribe to the ApplicationClosing event and cancel the event when you receive it
    
    internal class Module1 : Module
    {
    
      // Called by Framework when ArcGIS Pro is closing
      protected override bool CanUnload()
      {
        //return false to ~cancel~ Application close
        return false;
      }
    
      internal class Module2 : Module
      {
        public Module2()
        {
          ArcGIS.Desktop.Framework.Events.ApplicationClosingEvent.Subscribe(OnApplicationClosing);
        }
        ~Module2()
        {
          ArcGIS.Desktop.Framework.Events.ApplicationClosingEvent.Unsubscribe(OnApplicationClosing);
        }
    
        private Task OnApplicationClosing(System.ComponentModel.CancelEventArgs args)
        {
          args.Cancel = true;
          return Task.FromResult(0);
        }
    
        // cref: ARCGIS.DESKTOP.CORE.EVENTS.PROJECTOPENEDEVENT
        // cref: ARCGIS.DESKTOP.CORE.EVENTS.PROJECTOPENEDEVENT.SUBSCRIBE
        // cref: ARCGIS.DESKTOP.CORE.EVENTS.PROJECTOPENEDEVENT.UNSUBSCRIBE
        // cref: ARCGIS.DESKTOP.FRAMEWORK.CONTRACTS.MODULE.INITIALIZE
        // cref: ARCGIS.DESKTOP.FRAMEWORK.CONTRACTS.MODULE.UNINITIALIZE
        #region How to determine when a project is opened
        protected override bool Initialize() //Called when the Module is initialized.
        {
          ProjectOpenedEvent.Subscribe(OnProjectOpened); //subscribe to Project opened event
          return base.Initialize();
        }
    
        private void OnProjectOpened(ProjectEventArgs obj) //Project Opened event handler
        {
          MessageBox.Show($"{Project.Current} has opened"); //show your message box
        }
    
        protected override void Uninitialize() //unsubscribe to the project opened event
        {
          ProjectOpenedEvent.Unsubscribe(OnProjectOpened); //unsubscribe
          return;
        }
    Requirements

    Target Platforms: Windows 11, Windows 10, Windows 8.1

    See Also