Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How can I determine if I'm currently executing inside an ExternalEvent?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
jon.stelly
435 Views, 2 Replies

How can I determine if I'm currently executing inside an ExternalEvent?

Is there a helper method, static variable or flag somewhere that I can check to know if I'm executing inside of an ExternalEvent?  I've got code that is called from 2 places, one is already executing inside of the IExternalEventHandler, and the other is not.

 

I guess the real information I'm looking for is, 'is it safe to access Revit API objects or do I need to raise an ExternalEvent?'

 

This is a C#/.NET Addin.

Tags (1)
2 REPLIES 2
Message 2 of 3
arnostlobel
in reply to: jon.stelly

I do not think there is anything in the API for that exact purpose. ExternalEvent has a IsPending property, which would indicate whether a previously raised external events has been (or is being) processed or not. The flag gets reset before the event’s handler is executed, thus if you are expecting to get your handle executed you could check the flag. If it is True, you are not being called by Revit, and if it is False, you are. However, that only works if the event was raised (signaled), thus this is not a universal solution.

 

I think the simplest approach in your case is actually best done in your application. You could easily have two methods, one internal and one external. The external would be used as the external event's handler and it would do nothing but call the internal method with a flag passed as an argument. The flag you could then test in the internal method to see whether you are being called by the handler or by yourself.

 

As a side note, I am not sure I understand why you would want to raise an (another?) External Event in the scenario you described. However, the answer to the first part of the question is “no” – it is not safe nor recommended to call the Revit API from external threads / modeless dialogs except of a few very special cases, of which raising an External event is one.

Arnošt Löbel
Message 3 of 3
jon.stelly
in reply to: arnostlobel

Thanks Arnošt,

 

I don't want to raise the event twice.  I'm only raising the external event and executing code there so I can safely access Revit API objects, and I'm really just looking for a way to avoid raising the external event if I don't need to (if I'm already executing inside the ExternalEvent).

 

To give a bit more detail, this ExternalEvent that I've created is actually used similar to the WPF Dispatcher, I queue lambda actions or functions to be executed inside of the ExternalEvent.  I did this because the one method I mentioned in my original post is actually many, many methods that were written against Revit 2013 and 2014 when the thread checking was a little more relaxed.  Rather than rewrite a lot of that code, I wrap sections of code that access Revit API objects inside of a call that queues the work to be executed in a generic ExternalEvent.  Right now I'm just checking to see if I'm executing on the WPF UI thread, and I assume that I'm in a "safe to access Revit API objects" context at that point, but it would be nice if there were a simple blessed/supported API method to check that.  Something like the WPF Dispatcher.CheckAccess() method.

 

Thanks for the reply, this answers my question.

 

- Jon 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community