Detect if addon is already running - modeless form

Detect if addon is already running - modeless form

Kevin.Bell
Advisor Advisor
919 Views
7 Replies
Message 1 of 8

Detect if addon is already running - modeless form

Kevin.Bell
Advisor
Advisor

Hi,

 

I've created an addon which presents the user with a modeless form to do stuff. This works well.

 

The problem is that the user can click on the ribbon any number of times and run the addon - creates load of forms.

 

This isn't really a problem, its just messy.

 

Is there any way I can detect if the addon is already running and not display a new form?

 

I'm thinking that there would be something in the Application Class which would list active addons so I can check it to see if mine is already listed?

 

Thanks.

0 Likes
920 Views
7 Replies
Replies (7)
Message 2 of 8

Sean_Page
Collaborator
Collaborator

Could you use a simple bool (Open) variable in the calling command for the form? You could set it on execution to True and form close to False. It's not flashy, but should do the trick.

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 3 of 8

Kevin.Bell
Advisor
Advisor

I can't see how that would work, as each run is caused by the user clicking the addin from the Revit ribbon, I would think that one execution would not be able to 'see' the other?

 

The UIApplication class contains a method that will return all loaded applications:

 

            ExternalApplicationArray LoadedApplications;
            LoadedApplications = uiapp.LoadedApplications;

This works, and lists my application and others, but it does not tell you if the application is actually active or not (i.e. the user has clicked on it).

 

 

0 Likes
Message 4 of 8

Kennan.Chen
Advocate
Advocate

I think it's .NET rather than Revit API to make this happen. To ensure you only instantiate one form, just keep track of the created form and not creating another one when the same command runs. Specifically, use a static field to store the created form and check the reference next time you want to create again. Or you can make the form a singleton.

0 Likes
Message 5 of 8

Sean_Page
Collaborator
Collaborator

You just need the bool to be at the app level. I use bools like this for a variety of modeless items.

1. On command check bool status

2. On command set to true if false

3. On modeless form closing back to true

Sean Page, AIA, NCARB, LEED AP
Partner, Computational Designer, Architect
0 Likes
Message 6 of 8

jeremytammik
Autodesk
Autodesk

You can create and store your form in a singleton static class variable.

 

If the variable is null, the form has been closed and needs recreating if you wish to reopen it.

 

If the variable is non-null, the form exists and can be either hidden or shown.

 

I am pretty sure I have implemented that somewhere sometime in the past.

 

Maybe in the modeless loose connector add-in?

 

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 7 of 8

Kevin.Bell
Advisor
Advisor

OK, singleton form sounds like it could do the job.

 

I've never heard of that... will have to do a quick bit of learning.

 

Thanks for your replies guys.

0 Likes
Message 8 of 8

jeremytammik
Autodesk
Autodesk

Thank you for your appreciation. My pleasure entirely.

 

You can start here:

 

https://en.wikipedia.org/wiki/Singleton_pattern

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes