.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 8 (275 Views)
Accepted Solution

Avoiding duplicate Eventhandlers

275 Views, 7 Replies
09-09-2013 02:05 AM

In order to avoid duplicate eventhandlers, I did like the following...

 

Document doc = Application.DocumentManager.MdiActiveDocument;
try { doc.BeginDocumentClose -= new DocumentBeginCloseEventHandler(MyEventHandler); }
catch { // Do nothing }
doc.BeginDocumentClose += new DocumentBeginCloseEventHandler(MyEventHandler);

 

The above worked greatly with AutoCAD 2007.

But yesterday I migrated my code into AutoCAD 2010 (with Object ARX 2010).

 

Unfortunately, it seems like AutoCAD 2010 catches before my code catches it.... (Though I am not sure if this is even make sense)

 

The evidence...

 

error.PNG

 

If I ignore this message, everything works great as it was meant to be...

 

I am just wondering if I could disable(?) this. Or if there is any other effective work around....

Well, I guess I could just have a global boolean value that checks the status of event attachment.

But would this be the only way?

What about checking event handler was added or no?

Removing all custom handlers of an event

P.S.: I've not tested your's code.

Mentor
matus.brlit
Posts: 246
Registered: ‎03-11-2008
Message 2 of 8 (259 Views)

Re: Avoiding duplicate Eventhandlers

09-09-2013 04:29 AM in reply to: dynamicscope

do you get this message also in production, or just in debug mode?

Moderator
Alexander.Rivilis
Posts: 1,430
Registered: ‎04-09-2008
Message 3 of 8 (246 Views)

Re: Avoiding duplicate Eventhandlers

09-09-2013 06:16 AM in reply to: dynamicscope

Cannot remove .NET event handler


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 4 of 8 (216 Views)

Re: Avoiding duplicate Eventhandlers

09-09-2013 06:37 PM in reply to: Alexander.Rivilis

Thank you everyone~

 

@matus: Actually, I just tried and got the same message in production (release) mode.

 

@Alexander

 

In fact, the above code was simplified one. I will write it in more detail.

 

[CommandMethod("CreateObject")]
public static void CreateObject()
{
	// Creates a global object
	MyStaticClass.CreateObject();

	// When the current document is closing confirm user whether or not to destroy the object
	// This event handler is required only after CreateObject is called
	Document doc = Application.DocumentManager.MdiActiveDocument;
	try { doc.BeginDocumentClose -= new DocumentBeginCloseEventHandler(MyStaticClass.ConfirmDestroyObject); }
	catch { // Do nothing }
	doc.BeginDocumentClose += new DocumentBeginCloseEventHandler(MyStaticClass.ConfirmDestroyObject);
}

public static class MyStaticClass
{
	...
	
	internal static void ConfirmDestroyObject(object sender, DocumentBeginCloseEventArgs e)
	{
		Document doc = sender as Document;
		
		...
		
		System.Windows.Forms.DialogResult dr =
			System.Windows.Forms.MessageBox.Show(msg, "Alert", System.Windows.Forms.MessageBoxButtons.OKCancel);

		if (dr == System.Windows.Forms.DialogResult.OK)
		{
			MyStaticClass.DestroyObject();
		}
		else
		{
			e.Veto();                
		}
	}
}

 

My case... dose look like the static/shared issue...

or maybe am I missing something??

Moderator
Alexander.Rivilis
Posts: 1,430
Registered: ‎04-09-2008
Message 5 of 8 (202 Views)

Re: Avoiding duplicate Eventhandlers

09-09-2013 10:56 PM in reply to: dynamicscope

What about checking event handler was added or no?

Removing all custom handlers of an event

P.S.: I've not tested your's code.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Valued Contributor
FRFR1426
Posts: 87
Registered: ‎04-05-2012
Message 6 of 8 (195 Views)

Re: Avoiding duplicate Eventhandlers

09-09-2013 11:02 PM in reply to: dynamicscope

This smells like an AutoCAD bug. The same code does not throw any exception on AutoCAD 2014. The only way I see to avoid it is to put a boolean flag somewhere.

 

If you can make your CreateObject method an instance method, you can store this flag in a field of your command object. You can also use Document.UserData.

Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 7 of 8 (178 Views)

Re: Avoiding duplicate Eventhandlers

09-10-2013 12:01 AM in reply to: Alexander.Rivilis
The sameple works great. Except one.
At near end of the code...

"handler.Target" gives me "null"

So I had to just remove without checking assembly.

Hmmm.... Now I think about it. It could be a little dangerous if I don't check the assembly...
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 8 of 8 (165 Views)

Re: Avoiding duplicate Eventhandlers

09-10-2013 12:33 AM in reply to: dynamicscope
if ((handler.Method.Module.Assembly == System.Reflection.Assembly.GetExecutingAssembly()))

This works... =)
Problem solved~!!!

Thank you very much experts~!!
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.