Closing modeless when all documents are closed

Closing modeless when all documents are closed

SONA-ARCHITECTURE
Advocate Advocate
358 Views
3 Replies
Message 1 of 4

Closing modeless when all documents are closed

SONA-ARCHITECTURE
Advocate
Advocate

Hi,
Based on the Sdk sample called "ModelessForm_ExternalEvent", I run a modeless form to do some stuff

I'd like to close my modeless form when all documents are closed.

I've tried to do this but it seems that when I'm opening more than 2 documents, DoCumentClosed events do not raise.

What's wrong with my code please?

 

public class Main : IExternalApplication
    {
        // class instance
        internal static Main thisApp = null;
        // ModelessForm instance
        private RoutingManagerModelessForm m_RoutingManagerForm;
        public static Application m_App;

        #region external application public methods
        /// <summary>
        /// Called when Revit starts up
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>

        public Result OnStartup(UIControlledApplication application)
        {
            // Initialize whole plugin's user interface.
            var ui = new SetupInterface();
            ui.Initialize(application);            
            thisApp = this;
            m_RoutingManagerForm = null;

            return Result.Succeeded;
        }
        #endregion


        /// <summary>
        /// Called when Revit shutdown
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>

        public Result OnShutdown(UIControlledApplication application)
        {
            if (m_RoutingManagerForm !=null && m_RoutingManagerForm.IsVisible)
            {
                m_RoutingManagerForm.Close();
            }            
            return Result.Succeeded;
        }

        #region private methods

        
        /// <summary>
        ///   This method creates and shows a modeless dialog, unless it already exists.
        /// </summary>
        /// <remarks>
        ///   The external command invokes this on the end-user's request
        /// </remarks>
        /// 
        public void ShowForm(ExternalCommandData cD)
        {
            ExternalCommandData commandData = cD;
            m_App = cD.Application.Application;

            try
            { 
                // If we do not have a dialog yet, create and show it
                if (m_RoutingManagerForm == null || WindowHelper.IsDisposed(m_RoutingManagerForm)) 
                {
                    // We give the objects to the new dialog;
                    // The dialog becomes the owner responsible fore disposing them, eventually.
                    m_RoutingManagerForm = new RoutingManagerModelessForm(commandData);
                    m_RoutingManagerForm.myThisApp = this;
                    m_RoutingManagerForm.Show();

                    m_App.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(OnDocumentClosed);
                }
            }

            #region catch and finally
            catch (Exception ex)
            {
                ess_routing.Log.WriteDebug("EE_UpdateForm_ViewActivatedMethod" + Environment.NewLine + ex.Message + Environment.NewLine + ex.InnerException, true);
            }
            finally
            {
            }
            #endregion

        }

        private void OnDocumentClosed(object sender, DocumentClosedEventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Documents en cours:" + m_App.Documents.Size);

            if (m_RoutingManagerForm != null)
            {
                m_RoutingManagerForm.WakeUp();
                if (m_App.Documents.Size == 0)                                        
                    m_RoutingManagerForm.Close();
                
                m_App.DocumentClosed -= new EventHandler<DocumentClosedEventArgs>(OnDocumentClosed);
            }

        }

 

Pierre NAVARRA
SONA-Architecture.
http://www.sona-architecture.com
https://fr.linkedin.com/in/pierre-navarra-62032a107
0 Likes
Accepted solutions (1)
359 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

Very tricky to diagnose on sight. Maybe you are registering some event handlers on the wrong objects? I struggled with similar issues in various cases, and it was always a question of cleaning up and centralising. The fact that the DocumentClosed handler is not called at a certain point would seem to confirm this suspicion. I would suggest using standard debugging approaches:

 

  • divide and conquer
  • remove functionality step by step until it works
  • start from scratch with a working system and add functionality step by step until it stops working

  

Standard debugging tactics.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 4

SONA-ARCHITECTURE
Advocate
Advocate
Accepted solution

Thx Jeremy

I finally put the += new EventHandler on OnStartup instead of OnShutdown function and it works

public Result OnStartup(UIControlledApplication application)
        {
            // Initialize whole plugin's user interface.
            var ui = new SetupInterface();
            ui.Initialize(application);            
            thisApp = this;
            m_RoutingManagerForm = null;
            m_UIControlledApplication = application;

            application.ControlledApplication.DocumentClosing += new EventHandler<DocumentClosingEventArgs>(CheckClosingProject);
            
            return Result.Succeeded;
        }

 

Pierre NAVARRA
SONA-Architecture.
http://www.sona-architecture.com
https://fr.linkedin.com/in/pierre-navarra-62032a107
0 Likes
Message 4 of 4

jeremy_tammik
Alumni
Alumni

Yes. that makes sense. Congratulations on solving it.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes