.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

DocumentCollection and Database events issues

2 REPLIES 2
Reply
Message 1 of 3
_gile
524 Views, 2 Replies

DocumentCollection and Database events issues

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.

{code}
private DocumentCollection docMan = acadApp.DocumentManager;

public void Initialize()
{
docMan.DocumentCreated += new DocumentCollectionEventHandler(OnDocumentCreated);
docMan.DocumentToBeDestroyed += new DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
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);
try
{
docMan.DocumentCreated -= new DocumentCollectionEventHandler(OnDocumentCreated);
docMan.DocumentToBeDestroyed -= new DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("Terminte: " + ex.Message);
}
}

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

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);
}
}{code}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: _gile

Generally, you only need to unregister an event handler when the object
that's handling the event ceases to exist, rather than the object that fires
the event.

For Document, Editor, and Database events, you don't have to unregister
handlers of their events when the associated document is being destroyed,
because after that happens, all those objects will no longer exist and will
never fire events once they're gone.

That's not to suggest that you can't remove the handlers, you can, but if
you didn't it wouldn't matter.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

<_gile> wrote in message news:6281729@discussion.autodesk.com...
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.

{code}
private DocumentCollection docMan = acadApp.DocumentManager;

public void Initialize()
{
docMan.DocumentCreated += new
DocumentCollectionEventHandler(OnDocumentCreated);
docMan.DocumentToBeDestroyed += new
DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
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);
try
{
docMan.DocumentCreated -= new
DocumentCollectionEventHandler(OnDocumentCreated);
docMan.DocumentToBeDestroyed -= new
DocumentCollectionEventHandler(OnDocumentToBeDestroyed);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show("Terminte: " +
ex.Message);
}
}

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

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);
}
}{code}
Message 3 of 3
_gile
in reply to: _gile

Thanks Tony.
It makes sense.


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost