Question

Firstly I apologize for any typos etc. English is not my first language :)

I 've written an addin for Revit and now working on automatic updates for the addin. I already have the updates sorted but i stumbled upon small issue.

I'm checking for updates on a start of a Revit (when Revit creates the Addin application)

    public Result OnStartup(UIControlledApplication application)
    {
        Result result = CreateApplication(application);
        if (result == Result.Succeeded)
        {
            UpdateService updates = new UpdateService();
            try
            {
                updates.ManageUpdates(application);
            }
            catch (Exception ex)
            {
                NotificationManager.Notify("error", ex.InnerException.Message, messageType.error);
            }
        }
        return result;
    }

and if updates are available I'm showing a message to the user asking if he/she wants to update. If yes, I want to close Revit for them and start updating.

The problem is that at this point Revit did not finished loading itself and if i'll close it programmatically it will throw an Error, because it's not ready to be closed yet for another 5 seconds.

System.Threading.Thread.Sleep(5000);

Would solve the problem on my machine but not necesserily on the users, so i need a way of checking if the Revit is ready to be closed (in a state that allow for it to be closed).

So the question is: Is there a way to check if Revit can be closed? Has anyone had problem like this and solved it?

(i know i could check for the updates when the user closes Revit but it's not ideal...

Any ideas?

Was it helpful?

Solution

You could try subscribing to the ApplicationIntitialized event - I bet that only gets fired when Revit is fully loaded.

From the documentation:

The event is raised after Revit was launched as fully initialized, including initialization of external applications.

So... In your OnStartup method, do something like this:

public Result OnStartup(UIControlledApplication application)
{
    Result result = CreateApplication(application);
    if (result == Result.Succeeded)
    {
        application.Application.ApplicationInitialized += new EventHandler(delegate (Object o, ApplicationInitializedEventArgs a) {
            // this code will be deferred until the application is initialized
            UpdateService updates = new UpdateService();
            try
            {
                updates.ManageUpdates(application);
            }
            catch (Exception ex)
            {
                NotificationManager.Notify("error", ex.InnerException.Message, messageType.error);
            }
        });
    }
    return result;
}

EDIT: I didn't notice that I was referring to the Revit 2013 API reference and missed that 2012 doesn't have an ApplicationInitialized event. In this case, you could try to hook up to one of the DocumentOpening or Idling events, that will let you no that the application has initialized, but sadly at a moment when the user is just about to do something and that might make him mad.

It would be interesting to know if the Idling event fires without a document open - this might solve your problem. Could you try that out and report back?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top