ArcGIS Pro 3.3 API Reference Guide
ArcGIS.Desktop.Framework.Events Namespace / ApplicationClosingEvent Class / Subscribe Method
The method to call.
Subscribe with a strong reference. Default is false (weak reference).
Example

In This Topic
    Subscribe Method (ApplicationClosingEvent)
    In This Topic
    Call the specified method whenever the event occurs.
    Syntax

    Parameters

    action
    The method to call.
    keepSubscriberAlive
    Subscribe with a strong reference. Default is false (weak reference).

    Return Value

    A SubscriptionToken that can be used to unsubscribe with.
    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

    ArcGIS Pro version: 3 or higher.
    See Also