Skip to content

ricaun-io/ricaun.Revit.UI.Tasks

Repository files navigation

ricaun.Revit.UI.Tasks

Revit 2017 Visual Studio 2022 Nuke License MIT Build Release

ricaun.Revit.UI.Tasks

ricaun.Revit.UI.Tasks package allows using Task to run Revit API code asynchronously and await the code to finish.

This project was generated by the ricaun.AppLoader Revit plugin.

Features

  • Task: Run Revit API code asynchronously and await the code to finish.
  • InContext: Run code if in Revit Context instead of awaiting Task.
  • CancellationToken: Provides support for canceling asynchronous tasks.

RevitTaskService

The RevitTaskService is a service that manages the creation of the AsyncExternalEventHandler inside the Idling event.

UIApplication uiapp;
RevitTaskService revitTaskService = new RevitTaskService(uiapp);

or

UIControlledApplication application;
RevitTaskService revitTaskService = new RevitTaskService(application);

Initialize

Initialize the RevitTaskService to start the Idling event.

revitTaskService.Initialize();

Dispose

Dispose the RevitTaskService to stop the Idling event.

revitTaskService.Dispose();

IRevitTask

The RevitTaskService has the interface IRevitTask with the Run method to execute code in Revit Context.

IRevitTask revitTask = revitTaskService;

Run

The Run method runs the code inside Revit Context and await the code to finish with a CancellationToken.

CancellationToken token = CancellationToken.None;
UIApplication uiapp = await revitTask.Run((uiapp) =>
{
    // Code run inside Revit Context
    return uiapp;
}, token);

The IRevitTask interface has an extension methods for Run without UIApplication or CancellationToken and return.

await revitTask.Run(() => { });
await revitTask.Run(() => { return 1; });
await revitTask.Run((uiapp) => { });
await revitTask.Run((uiapp) => { return 1; });
await revitTask.Run(() => { }, token);
await revitTask.Run(() => { return 1; }, token);
await revitTask.Run((uiapp) => { }, token);

Example

Example sample to implement RevitTaskService within IExternalApplication.

public class App : IExternalApplication
{
    private static RevitTaskService revitTaskService;
    public static IRevitTask RevitTask => revitTaskService;
    public Result OnStartup(UIControlledApplication application)
    {
        revitTaskService = new RevitTaskService(application);
        revitTaskService.Initialize();

        Task.Run(async () =>
        {
            await Task.Delay(1000);
            await RevitTask.Run(() =>
            {
                TaskDialog.Show("Revit", "Hello from RevitTask");
            });
        });

        return Result.Succeeded;
    }

    public Result OnShutdown(UIControlledApplication application)
    {
        revitTaskService?.Dispose();

        return Result.Succeeded;
    }
}

Similar Projects

There are some similar packages/implementations to run Revit API code in an async way using await/Tasks.

Release

License

This project is licensed under the MIT Licence.


Do you like this project? Please star this project on GitHub!