C#.net Addin Event Handler Function

C#.net Addin Event Handler Function

Anonymous
Not applicable
868 Views
3 Replies
Message 1 of 4

C#.net Addin Event Handler Function

Anonymous
Not applicable
Hi,

I am writing an Addin for Inv2010, and I am trying to implement an event handler function for the "OnDocumentChange" application event. I can't figure out what is wrong with my code. I am using the Addin wizard as a base for my project, this code is in the StandardAddInServer.cs file.

I've simplified the code in my ApplicationEvents_OnDocumentChange handler function. I know the event is firing because I can capture it with the EventWatcher program supplied with the SDK. Debug shows that the program execution never enters my handler function. What am I doing wrong?

public void Activate(Inventor.ApplicationAddInSite addInSiteObject, bool firstTime)
{
// This method is called by Inventor when it loads the addin.
// The AddInSiteObject provides access to the Inventor Application object.
// The FirstTime flag indicates if the addin is loaded for the first time.

// Initialize AddIn members.
m_inventorApplication = addInSiteObject.Application;

// Initialize event handlers
m_inventorApplication.ApplicationEvents.OnDocumentChange += new ApplicationEventsSink_OnDocumentChangeEventHandler(ApplicationEvents_OnDocumentChange);
.
. snip
.
}


void ApplicationEvents_OnDocumentChange(_Document DocumentObject, EventTimingEnum BeforeOrAfter, CommandTypesEnum ReasonsForChange, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
MessageBox.Show("Event Text");
HandlingCode = HandlingCodeEnum.kEventHandled;
}
0 Likes
869 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
I just tried putting this code into a new addin, and it works fine. What could cause this problem with my original addin?
0 Likes
Message 3 of 4

jeff.pek
Community Manager
Community Manager
Hi -

When subscribing to events from .NET, you need to also hold on to a reference to the xxxEvents object (in this case, ApplicationEvents). Otherwise, the temporary reference you used to subscribe to the event gets garbage-collected.

Jeff
0 Likes
Message 4 of 4

Anonymous
Not applicable
You're right, that fixed the issue. Thank you!
0 Likes