Use Await Async Revit API

The Revit API provides an External Events framework to accommodate the use of modeless dialogs. It is tailored for asynchronous processing and operates similarly to the Idling event with default frequency. You can look at the official document from Autodesk here : https://help.autodesk.com/view/RVT/2024/ENU/?guid=Revit_API_Revit_API_Developers_Guide_Advanced_Topics_External_Events_html But we can use more simple than with Revit.Async Use In IExternalCommand Dowload Library Revit.Async from package nuget API manager Add Test Code: using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Revit.Async; namespace UpdateAssemblyCodeAddIn; [Transaction(TransactionMode.Manual)] public class Command : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { var externalCommandData = commandData; string zipFilePath = string.Empty; RevitTask.Initialize(externalCommandData.Application); RevitTask.RunAsync(async () => { UIDocument uidoc = externalCommandData.Application.ActiveUIDocument; Document doc = uidoc.Document; await Task.Delay(1000); }); return Result.Succeeded; } } Use In ExternalEventHandler public Result OnStartup(UIControlledApplication application) { UIApplication uiapp = application.GetUIApplication(); RevitTask.Initialize(uiApp); return Result.Succeeded; } Some diffrence way to get UIApplication is can use a method follow with topic Revit API Forum /// /// Get using the /// /// Revit UIApplication public static UIApplication GetUIApplication(this UIControlledApplication application) { var type = typeof(UIControlledApplication); var propertie = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .FirstOrDefault(e => e.FieldType == typeof(UIApplication)); return propertie?.GetValue(application) as UIApplication; } Use In IExternalDBApplication public Result OnStartup(UIControlledApplication application) { application.ControlledApplication.ApplicationInitialized += test; return Result.Succeeded; } private void test(object sender, ApplicationInitializedEventArgs e) { if (sender is Application application) { var uiApp = new UIApplication(application); RevitTask.Initialize(uiApp); } }

Jan 18, 2025 - 04:34
Use Await Async Revit API

The Revit API provides an External Events framework to accommodate the use of modeless dialogs. It is tailored for asynchronous processing and operates similarly to the Idling event with default frequency.

You can look at the official document from Autodesk here : https://help.autodesk.com/view/RVT/2024/ENU/?guid=Revit_API_Revit_API_Developers_Guide_Advanced_Topics_External_Events_html

But we can use more simple than with Revit.Async

Use In IExternalCommand

  1. Dowload Library Revit.Async from package nuget API manager
<PackageReference Include="Revit.Async" Version="2.1.*" />
  1. Add Test Code:
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Revit.Async;

namespace UpdateAssemblyCodeAddIn;

[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
    {
        var externalCommandData = commandData;
        string zipFilePath = string.Empty;
        RevitTask.Initialize(externalCommandData.Application);
        RevitTask.RunAsync(async () =>
        {
            UIDocument uidoc = externalCommandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            await Task.Delay(1000);

        });
        return Result.Succeeded;
    }

}

Use In ExternalEventHandler

public Result OnStartup(UIControlledApplication application)
{
    UIApplication uiapp = application.GetUIApplication();
    RevitTask.Initialize(uiApp);
    return Result.Succeeded;
}

Some diffrence way to get UIApplication is can use a method follow with topic Revit API Forum

/// 
/// Get  using the 
/// 
/// Revit UIApplication
public static UIApplication GetUIApplication(this UIControlledApplication application)
{
    var type = typeof(UIControlledApplication);

    var propertie = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
        .FirstOrDefault(e => e.FieldType == typeof(UIApplication));

    return propertie?.GetValue(application) as UIApplication;
}

Use In IExternalDBApplication

public Result OnStartup(UIControlledApplication application)
   {
       application.ControlledApplication.ApplicationInitialized  += test;
       return Result.Succeeded;
   }

   private void test(object sender, ApplicationInitializedEventArgs e)
   {
       if (sender is Application application)
       {
           var uiApp = new UIApplication(application);
           RevitTask.Initialize(uiApp);
       }
   }