Registering Idling Event on external thread (non Revit UI thread)

Registering Idling Event on external thread (non Revit UI thread)

luisPDAN6
Advocate Advocate
944 Views
2 Replies
Message 1 of 3

Registering Idling Event on external thread (non Revit UI thread)

luisPDAN6
Advocate
Advocate

We find 2 examples on the SDK for modeless forms, one with external events and the other with idling. The issue with the idling event example is that the event is registered once and staying that way. I would like to register and unregister that event on demand, to prevent my code listening to all the calls of the idling event (inspired by a post from Jeremy) . So, being a newbie I have tried to make a simple call register the event outside the Revit API context with:

_cachedUiCtrApp.Idling += new EventHandler<IdlingEventArgs>(OnIdling);

 

And got the exception:

Invalid call to Revit API! Revit is currently not within an API context.

 

What do we need to implement in the non UI Revit thread to register the idling event correctly?

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

RPTHOMAS108
Mentor
Mentor
Accepted solution

You can subscribe to idling within a valid context such as:

IExternalApplication.OnStartUp

IExternalCommand.Execute

IExternalEventHandler.Execute (not sure I would be doing it from here though).

 

You can't subscribe to it on a separate thread.

 

The default behaviour of idling is that it raises just once when moving to an idle state. The easiest way to get it to behave as you state is to subscribe to it then within it's handler unsubscribe to it. There is no real need for anything special beyond that i.e. separate threads etc.

 

i.e.

I have a handler being raised continuously or occasionally

I have a static variable that tells the handler if I want to unsubscribe on that occasion or not

I unsubscribe and reset variable (no more event until next subscription to it) 

 

I don't like the look of the name '_cachedUiCtrApp' since it infers that you are storing something you don't need to.

Message 3 of 3

luisPDAN6
Advocate
Advocate
Thank you. I endup using IExternalEventHandler.Execute - it works with great stability. I dont need the _cachedUiCtrApp, I use the uiapp directly.
0 Likes