When a Revit ExternalEvent is raised, ExternalEvent should execute before my code.

When a Revit ExternalEvent is raised, ExternalEvent should execute before my code.

dineshkXQCS9
Explorer Explorer
543 Views
2 Replies
Message 1 of 3

When a Revit ExternalEvent is raised, ExternalEvent should execute before my code.

dineshkXQCS9
Explorer
Explorer

I'm facing an issue with making ExternalEvent execute before my code. 

Context of the Problem : 
I have WPF UI which is called when I click the push button on the ribbon, this UI in turn calls the "ExternalEvent" which runs the test on the document and we use the test results to perform the action. 

 

Current State of Problem:
As a work around we are using 2 push buttons one to call external event and then we call the UI, this is not efficient workflow for us and we would like to execute this as a part of single push button.
Please let us know what solution could we explore? 

 

 

/*When the button is clicked in the fucntion below the External event should execute first , but it is not happening instead it complete all other tasks and then run external event handler. */

//Function

        private void BtnClicked()
        {
            btnClickedEvent.Raise();
            Othertasks();
        }
//external Event

btnClickedEvent = ExternalEvent.Create(new ExternalEventhandle());

    public class ExternalEventhandle: IExternalEventHandler
    {
        public void Execute(UIApplication app)
        {
            UIDocument UIdoc = app.ActiveUIDocument;
            Document doc = UIdoc.Document;
            //----some task to do ------
            RunTestFunction();
        }

        public string GetName()
        {
            return "";
        }
    }
/Expectation: Want to excute RunTestFunction() first , only when it is completed then only Run Othertasks() function/

 

 

Accepted solutions (2)
544 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni
Accepted solution

Implement an external command CmdActionPart2 for part 2, the action to execute after the external event.

  

In the external event Execute method, at the end, just before returning, after part 1 of your action has completed, call PostCommand and invoke CmdActionPart2.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 3

mhannonQ65N2
Collaborator
Collaborator
Accepted solution

Are you raising the external event directly from your command? If so, you might not need to use an external event and can just do what you want directly (i.e. call RunTestFunction()).

 

If this is in a WPF window, then it depends on whether it is modal or modeless. For a modal window, it is the same as if it is directly in a command--you might be able to do what you want directly. For a modeless window, you should look at RevitAsync, by @Anonymous. Using RevitAsync, you could either await the task wrapping the external event then do Othertasks() or, if you want your UI to be responsive while the external event is running, use ContinueWith(() => Othertasks()).