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.
- Task: Run
Revit API
code asynchronously andawait
the code to finish. - InContext: Run code if in
Revit Context
instead of awaitingTask
. - CancellationToken: Provides support for canceling asynchronous tasks.
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 the RevitTaskService
to start the Idling
event.
revitTaskService.Initialize();
Dispose the RevitTaskService
to stop the Idling
event.
revitTaskService.Dispose();
The RevitTaskService
has the interface IRevitTask
with the Run
method to execute code in Revit Context.
IRevitTask revitTask = revitTaskService;
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 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;
}
}
There are some similar packages/implementations to run Revit API code in an async way using await/Tasks.
This project is licensed under the MIT Licence.
Do you like this project? Please star this project on GitHub!