Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhook events when unloading addin #54

Open
brinkdinges opened this issue Aug 13, 2018 · 4 comments
Open

Unhook events when unloading addin #54

brinkdinges opened this issue Aug 13, 2018 · 4 comments

Comments

@brinkdinges
Copy link
Contributor

Hi guys,

My add-in for creating a new drawing with one click uses events, for example when the active model changes, to show a different panel in the task pane. I have noticed that these events stay hooked after the add-in is unloaded.

Is this behavior expected? What would be the best way to unhook from these events? I already tried:

  • Adding TaskPaneIntegration.RemoveFromTaskpane() in DisconnectedFromSolidWorks, but I checked the code and noticed the form isn't destroyed.
  • Adding a destructor to the form and to its view model. These only get called when SolidWorks closes.

Any ideas how to best handle this?

@mtbayley
Copy link
Contributor

mtbayley commented Aug 15, 2018

I have noticed this issue as well. Luckily we keep the add-in always enabled / loaded, so this isn't really a problem for us.

@angelsix
Copy link
Owner

Can you provide some sample code/add-in?

@brinkdinges
Copy link
Contributor Author

I finally made an example: https://github.com/brinkdinges/solidworks-api-events-example/commits/master

It shows an popup every time the active model info changes. When you load the add-in, then unload it, the popup still shows up. It's only not shown when the last document is closed.

@brinkdinges
Copy link
Contributor Author

brinkdinges commented Dec 24, 2019

I've been looking into this issue again and I think we shouldn't try to solve it in SolidDna.

I assume most add-ins will use the ActiveModelInformationChanged event or one of the other events in SolidWorksApplication. We cannot clear the InvocationList for these events, because multiple add-ins might use SolidDna and we would have to remove certain events. We also don't know anything about their WPFControl, so we cannot set the DataContext to null or something like that (if that would even unhook the events).

I have solved it for my add-in by storing the task pane view model instance. When DisconnectedFromSolidWorks is called, I call a ShutDown method on the instance to unsubscribe from the events.

Edit: example here, commit 7801685

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants