Author Topic: DocumentCollection and Database events issues  (Read 2022 times)

0 Members and 1 Guest are viewing this topic.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
DocumentCollection and Database events issues
« on: October 31, 2009, 07:22:52 PM »
Hi,

I want my application to display a message on opening a new document and on saving the active document.
So, in the Initialize method, I register a DocumentCollection.DocumentCreated and a Database.BeginSave one for all already opened documents.
The DocumentCreated callback method registers a Database.BeginSave for the new Document.Databse.

My questions are:
1- Do I need to unregister the DocumentCollection.DocumentCreated in the terminate method ?
2- Do I need to add a DocumentCollection.DocumentToBeDestroyed event with a callback to unregister the Database.BeginSave of this document ?
3- If so, do I need to unregister the DocumentCollection.DocumentToBeDestroyed in the Terminate method too ?

Here's an extract of what I wrote (the complete code is here)

Code: [Select]
        private DocumentCollection docMan = acadApp.DocumentManager;

        public void Initialize()
        {
            docMan.DocumentCreated += new DocumentCollectionEventHandler(OnDocumentCreated);
            [color=red]docMan.DocumentToBeDestroyed += new DocumentCollectionEventHandler(OnDocumentToBeDestroyed);[/color]
            foreach (Document doc in docMan)
            {
                doc.Database.BeginSave += new DatabaseIOEventHandler(OnSaveAlert);
            }
            AddDefaultContextMenu();
            AddObjectContextMenu();
            ShowMessage();
        }

        public void Terminate()
        {
            acadApp.RemoveDefaultContextMenuExtension(dcme);
            RXClass rxc = Entity.GetClass(typeof(Entity));
            acadApp.RemoveObjectContextMenuExtension(rxc, ocme);
            [color=red]try
            {
                docMan.DocumentCreated -= new DocumentCollectionEventHandler(OnDocumentCreated);
                docMan.DocumentToBeDestroyed -= new DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Terminte: " + ex.Message);
            }[/color]
        }

        private void OnDocumentCreated(object sender, DocumentCollectionEventArgs e)
        {
            if (e.Document == null) return;
            e.Document.Database.BeginSave += new DatabaseIOEventHandler(OnSaveAlert);
            ShowMessage();
        }

        [color=red]private void OnDocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
        {
            Document doc = e.Document;
            try
            {
                doc.Database.BeginSave -= new DatabaseIOEventHandler(OnSaveAlert);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("DocumentToBeDestroyed: " + ex.Message);
            }
        }[/color]
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: DocumentCollection and Database events issues
« Reply #1 on: November 02, 2009, 02:50:47 AM »
Hi,

Tony replies the same question on Autodesk NG.
It seems that I wrote some superfluous code, as there's no need to unregister these events handlers.
Speaking English as a French Frog