Hi! I want to create ExternalEvent on button click. But I get exception: "Attempting to create an ExternalEvent outside of a standard API execution". When I checked the thread of the main window and the Command class, they were the same thread. What's the magic? I created the class for reseting event in Command class.
Look at the Revit SDK sample ModelessDialog/ModelessForm_ExternalEvent.
The Building Coder also shares lots of samples implementing external events:
https://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28
I found this site to be exceptionally helpful when creating external event handlers.
You cannot call ExternalEvent.Create() outside of a valid Revit API context, and a callback from a button click is most definitely not a valid context.
What i do is create an instance of the IExternalEventHandler and call ExternalEvent.Create() from the IExternalCommand, or other valid context, and pass them to the viewmodel (or create them in the ViewModel contstructor if you're newing up the ViewModel in the IExternalCommand. I prefer MVVM, if you do not then pass them to or create them where you're UI logic resides, a Controller, or directly in the Form, or wherever. Then in your button click callback you pass necessary state info to your IExternalEventHandler and then call Raise().
Dear Bobby and Sean,
Thank you very much for your very helpful input.
I added it to The Building Coder for future reference:
I also made a local copy the of the knowledge base article, since it is marked as Revit 2016 and I worry about it getting lost otherwise.
Cheers,
Jeremy
Can't find what you're looking for? Ask the community or share your knowledge.