ApplicationEvents losing my Code

ApplicationEvents losing my Code

bindlmi
Advocate Advocate
265 Views
2 Replies
Message 1 of 3

ApplicationEvents losing my Code

bindlmi
Advocate
Advocate

Hi,
I have a problem with my C# addin and Inventor 2023.3.1. I didn't create the addin from scratch, I just maintained it. I think it's already .NET 8.0 (but I'm not sure)

 

However, the addin is loaded and the ApplicationEvents OnSaveDocument and OnDocumentChange also work at first. After some time or some actions (I couldn't find out when exactly) these events don't trigger my code anymore, as if Inventor had lost this assignment. When I unload and reload the addin it works again for a while...

 

Does anyone have any ideas/advice on how I can continue to troubleshoot this? I already had several MessageBoxes at the beginning and at the end of my code, but at some point they just stopped appearing.

 

If you want me to upload any files, please tell me which ones. (I can program a bit, but only know a little bit about the whole thing)

 

I am grateful for any help 🙂

0 Likes
Accepted solutions (1)
266 Views
2 Replies
Replies (2)
Message 2 of 3

Michael.Navara
Advisor
Advisor
Accepted solution

Usually this is because you don't keep the reference to the ApplicationEvents object. Look at following samples if it is your case.

 

Bad sample - Don't do this

public class SampleAddIn_Bad : ApplicationAddInServer
{

    private Inventor.Application inventorApp;

    public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
    {
        inventorApp = addInSiteObject.Application;

        //This event handler will be cleaned up by GarbageCollector
        inventorApp.ApplicationEvents.OnSaveDocument += ApplicationEvents_OnSaveDocument;
    }

    private void ApplicationEvents_OnSaveDocument(_Document documentObject, EventTimingEnum beforeOrAfter, NameValueMap context, out HandlingCodeEnum handlingCode)
    {
        //Do something here

        handlingCode = HandlingCodeEnum.kEventNotHandled;
    }
...
}

 

Correct sample

public class SampleAddIn_Correct : ApplicationAddInServer
{

    private Inventor.Application inventorApp;
    private ApplicationEvents applicationEvents;

    public void Activate(ApplicationAddInSite addInSiteObject, bool firstTime)
    {
        inventorApp = addInSiteObject.Application;

        //Keep the reference to the ApplicationEvents object at add-in level
        applicationEvents = inventorApp.ApplicationEvents;
        applicationEvents.OnSaveDocument += ApplicationEvents_OnSaveDocument;
    }

    private void ApplicationEvents_OnSaveDocument(_Document documentObject, EventTimingEnum beforeOrAfter, NameValueMap context, out HandlingCodeEnum handlingCode)
    {
        //Do something here

        handlingCode = HandlingCodeEnum.kEventNotHandled;
    }
...
}

 

0 Likes
Message 3 of 3

bindlmi
Advocate
Advocate

Hi,
thanks for the tip. I had the bad example of course.

I will test this and observe the next few days - feedback will follow

0 Likes