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

Avoiding duplicate Eventhandlers

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
dynamicscope
983 Views, 7 Replies

Avoiding duplicate Eventhandlers

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?

7 REPLIES 7
Message 2 of 8
matus.brlit
in reply to: dynamicscope

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

Message 3 of 8

Cannot remove .NET event handler

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 4 of 8

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??

Message 5 of 8

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.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 6 of 8
FRFR1426
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.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
Message 7 of 8

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...
Message 8 of 8
dynamicscope
in reply to: dynamicscope

if ((handler.Method.Module.Assembly == System.Reflection.Assembly.GetExecutingAssembly()))

This works... 😃
Problem solved~!!!

Thank you very much experts~!!

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